RichTextBox 查找内容并红色显示
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); } }