What is gonna wrong in this code . i want to Highlight "This" word in rich text box .
I have uploaded the code and also the code is listed below :
private int SubStr(int start,string a, string b)
{
int ret = -1;
if (a.Substring(start).Contains(b))
{
ret = a.IndexOf(b[0]);
}
return ret;
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
start = richTextBox1.SelectionStart;
Pos = SubStr(last, richTextBox1.Text, "This");
if (Pos != -1)
{
richTextBox1.Select(Pos, 4);
richTextBox1.SelectionColor = Color.Blue;
richTextBox1.DeselectAll();
last += 4;
}
richTextBox1.SelectionColor = Color.Black;
richTextBox1.SelectionStart = start;
}
Loading

Kirtan PatelPosted Jan 10, 2010, 4:22 PM
using System.Text.RegularExpressions;
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
Regex rex = new Regex("This");
int selectionstart = richTextBox1.SelectionStart;
foreach (Match m in rex.Matches(richTextBox1.Text))
{
richTextBox1.Select(m.Index, m.Value.Length);
richTextBox1.SelectionColor = Color.Blue;
richTextBox1.SelectionStart = selectionstart;
richTextBox1.SelectionColor = Color.Black;
}
}