winform 多个关键字的高亮显示

    在textBox1输入几个关键字,点击button1查询,这几个关键字在 richTextBox1中高亮显示,代码如下:

        private string GaoL = null;

        private void HighLightText()
        {
            string ss = textBox1.Text;
            string[] s = ss.Split(' ');

            foreach (string d in s)
            {
                GaoL = d;
                richTextBox1.SelectAll();
                Regex r = new Regex(GaoL, RegexOptions.IgnoreCase);
                foreach (Match m in r.Matches(richTextBox1.Text))
                {
                    richTextBox1.Select(m.Index, m.Length);
                    richTextBox1.SelectionColor = Color.Red;
                    richTextBox1.SelectionFont = new Font("楷体", 12, FontStyle.Bold);
                }
              
            }          
        }

        private void button1_Click(object sender, EventArgs e)
        {
            HighLightText();
        }

 效果图:

  

posted @ 2011-08-22 09:19  欧阳三寿  阅读(1302)  评论(1编辑  收藏  举报