Hi Michael,
I performed a test based on your sample code and did see the problem on my
side. But I don't think the problem is caused by the timer tick occurring
on a thread other than the UI thread. It is UI thread that executes the
timer's Tick event handler when the timer's Tick event occurs.
I think the reason of the problem is that you're attempting to
select/unselect the text in the RichTextBox while it gets focused, which
causes the window flash/scroll.
I suggest that you remove the timer in your project and only check the
words near the current input cursor to see if they match the key words, if
yes, select them and change their color. In this way, you needn't always
re-check ALL the text and re-hilight those key words.
The following is a sample. It requires that you add a RichTextBox control
on the form.
public partial class Form1 : Form
{
string[] myWordList = new string[] { "the", "a", "an" };
Color hilightColor = Color.Orange;
public Form2()
{
InitializeComponent();
this.richTextBox1.TextChanged += new
EventHandler(richTextBox1_TextChanged);
}
void richTextBox1_TextChanged(object sender, EventArgs e)
{
if (this.richTextBox1.Text.Trim() != "")
{
if (this.GetCharBeforeSelectionStart() == 0)
{
this.ProcessNextWord(this.richTextBox1.SelectionSt art);
}
if (this.GetCharBeforeSelectionStart() == ' ' ||
this.GetCharBeforeSelectionStart() == '\n')
{
this.ProcessPreWord(this.richTextBox1.SelectionSta rt);
this.ProcessNextWord(this.richTextBox1.SelectionSt art);
}
else
{
this.ProcessCurrentWord(this.richTextBox1.Selectio nStart);
}
}
}
private char GetCharBeforeSelectionStart()
{
if (this.richTextBox1.SelectionStart == 0)
{
return '0';
}
else
{
return
this.richTextBox1.Text[this.richTextBox1.SelectionStart - 1];
}
}
private bool IsMatchKeyword(string text)
{
int i = 0;
for (; i < myWordList.Length; i++)
{
if (myWordList[i] == text)
{
break;
}
}
if (i < myWordList.Length)
return true;
else
return false;
}
private void ProcessCurrentWord(int carsetIndex)
{
int wordstart = carsetIndex -1;
while ((wordstart - 1 >= 0) &&
(this.richTextBox1.Text[wordstart - 1] != ' ' &&
this.richTextBox1.Text[wordstart-1]!='\n'))
{
wordstart--;
}
int wordend = carsetIndex - 1;
while ((wordend + 1 < this.richTextBox1.TextLength) &&
this.richTextBox1.Text[wordend + 1] != ' ' &&
this.richTextBox1.Text[wordend +1] != '\n')
{
wordend++;
}
string wordtext = this.richTextBox1.Text.Substring(wordstart,
wordend - wordstart + 1);
this.richTextBox1.SelectionStart = wordstart;
this.richTextBox1.SelectionLength = wordend - wordstart + 1;
if (IsMatchKeyword(wordtext))
{
this.richTextBox1.SelectionColor = this.hilightColor;
}
else
{
this.richTextBox1.SelectionColor =
this.richTextBox1.ForeColor;
}
this.richTextBox1.SelectionStart = carsetIndex;
this.richTextBox1.SelectionLength = 0;
}
private void ProcessPreWord(int carsetIndex)
{
int wordend = carsetIndex -1;
while (wordend >= 0 && (this.richTextBox1.Text[wordend] == ' '
|| this.richTextBox1.Text[wordend] == '\n'))
{
wordend--;
}
int wordstart = wordend;
if (wordstart 0)
{
while ((wordstart - 1 >= 0) &&
this.richTextBox1.Text[wordstart-1] != ' ' &&
this.richTextBox1.Text[wordstart-1]!='\n')
{
wordstart--;
}
}
if (wordstart >= 0)
{
this.richTextBox1.SelectionStart = wordstart;
this.richTextBox1.SelectionLength = wordend - wordstart + 1;
string wordtext =
this.richTextBox1.Text.Substring(wordstart, wordend - wordstart + 1);
if (IsMatchKeyword(wordtext))
{
this.richTextBox1.SelectionColor = this.hilightColor;
}
else
{
this.richTextBox1.SelectionColor =
this.richTextBox1.ForeColor;
}
this.richTextBox1.SelectionStart = carsetIndex;
this.richTextBox1.SelectionLength = 0;
}
}
private void ProcessNextWord(int carsetIndex)
{
int wordstart = carsetIndex -1;
while ((wordstart < this.richTextBox1.TextLength) &&
(this.richTextBox1.Text[wordstart] == ' ' ||
this.richTextBox1.Text[wordstart]=='\n'))
{
wordstart++;
}
int wordend = wordstart;
if (wordend < this.richTextBox1.TextLength - 1)
{
while (wordend + 1 < this.richTextBox1.TextLength &&
this.richTextBox1.Text[wordend +1]!= ' '&&
this.richTextBox1.Text[wordend+1]!='\n')
{
wordend++;
}
}
if (wordstart < this.richTextBox1.TextLength)
{
this.richTextBox1.SelectionStart = wordstart;
this.richTextBox1.SelectionLength = wordend - wordstart + 1;
string wordtext = this.richTextBox1.Text.Substring
(wordstart,wordend - wordstart +1);
if (IsMatchKeyword(wordtext))
{
this.richTextBox1.SelectionColor = this.hilightColor;
}
else
{
this.richTextBox1.SelectionColor =
this.richTextBox1.ForeColor;
}
this.richTextBox1.SelectionStart = carsetIndex;
this.richTextBox1.SelectionLength = 0;
}
}
}
Hope this helps.
If you have anything unclear, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.