码迷,mamicode.com
首页 > 其他好文 > 详细

RichTextBox 查找内容并红色显示

时间:2018-09-08 23:32:27      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:cti   col   rtx   vat   sele   red   sea   while   img   

RichTextBox 中打开文件并显示,按查找,匹配所有相同的文字并红色显示:

private void BtnSearch_Click(object sender, EventArgs e)
        {
            string strKeyWords = txtKeyWords.Text.Trim();
            //int intFirst = rtxtbox.Text.IndexOf(strKeyWords);
            //if (intFirst >= 0)
            //{
            //    rtxtbox.Select(intFirst, strKeyWords.Length );
            //    rtxtbox.SelectionColor = Color.Red;
            //}
            Regex regex = new Regex(strKeyWords);
            Match match = regex.Match(rtxtbox.Text);
            while (match.Success)
            {
                rtxtbox.Select(match.Index, strKeyWords.Length);
                rtxtbox.SelectionColor = Color.Red;
                match = regex.Match(rtxtbox.Text, match.Index + strKeyWords.Length);
            }
        }

技术分享图片

RichTextBox 查找内容并红色显示

标签:cti   col   rtx   vat   sele   red   sea   while   img   

原文地址:https://www.cnblogs.com/wenjie0904/p/9610918.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!