RichTextBox中如查找所有符合条件的字符串

jxqihan 2010-06-18 04:03:01
RichTextBox中如查找所有符合条件的字符串,并用底色标出,点击其它地方时底色消失。
...全文
254 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
qiuxue110 2010-11-18
  • 打赏
  • 举报
回复
string find = "string";
for (int i = 0; i < this.richTextBox1.Text.Length;i++ )
{
int index = this.richTextBox1.Find(find,i,RichTextBoxFinds.MatchCase);
this.richTextBox1.SelectionBackColor = Color.Red;
}
qiuxue110 2010-11-18
  • 打赏
  • 举报
回复
string find = "string";
for (int i = 0; i < this.richTextBox1.Text.Length;i++ )
{
int index = this.richTextBox1.Find(find,i,RichTextBoxFinds.MatchCase);
this.richTextBox1.SelectionBackColor = Color.Red;
}
wxm3630478 2010-06-19
  • 打赏
  • 举报
回复
mark。。。。。
兔子-顾问 2010-06-19
  • 打赏
  • 举报
回复
2楼就已经引用了2楼的回复,而且还是客客的。但实际5楼才出现客客。难道是csdn的bug?
-过客- 2010-06-19
  • 打赏
  • 举报
回复
呵呵,这个是要根据实际情况调整的

string key = Regex.Escape(textBox1.Text);
Regex reg = new Regex(key);
MatchCollection mc = reg.Matches(yourStr);
foreach (Match m in mc)
{
richTextBox1.SelectionStart = m.Index;
richTextBox1.SelectionLength = m.Value.Length;
richTextBox1.SelectionBackColor = Color.LightBlue;
}
//如果要忽略大小写,正则替换为
Regex reg = new Regex(@"(?i)" + key);
wuyq11 2010-06-18
  • 打赏
  • 举报
回复
Font f=new Font("Verdana", 8F, FontStyle.Italic, GraphicsUnit.Point);
string str="";
if (richTextBox1.Find(str)>0)
{
int pos=richTextBox1.Find(str);
richTextBox1.SelectionStart=pos;
richTextBox1.SelectionLength=str.Length;
richTextBox1.SelectionFont=f;
richTextBox1.SelectionColor=Color.Red;
}
铛铛 2010-06-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lxcnn 的回复:]
string key = Regex.Escape(textBox1.Text);
Regex reg = new Regex(@"(?m)^" + key);
MatchCollection mc = reg.Matches(yourStr);
foreach (Match m in mc)
{
richTextBox1.SelectionStart = m.Index;
richTextBox1.SelectionLength = m.Value.Length;
richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, FontStyle.Bold);
}
[/Quote]
捷哥1999 2010-06-18
  • 打赏
  • 举报
回复

111,101

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧