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()
{
InitializeCompo nent();
this.richTextBo x1.TextChanged += new
EventHandler(ri chTextBox1_Text Changed);
}
void richTextBox1_Te xtChanged(objec t sender, EventArgs e)
{
if (this.richTextB ox1.Text.Trim() != "")
{
if (this.GetCharBe foreSelectionSt art() == 0)
{
this.ProcessNex tWord(this.rich TextBox1.Select ionStart);
}
if (this.GetCharBe foreSelectionSt art() == ' ' ||
this.GetCharBef oreSelectionSta rt() == '\n')
{
this.ProcessPre Word(this.richT extBox1.Selecti onStart);
this.ProcessNex tWord(this.rich TextBox1.Select ionStart);
}
else
{
this.ProcessCur rentWord(this.r ichTextBox1.Sel ectionStart);
}
}
}
private char GetCharBeforeSe lectionStart()
{
if (this.richTextB ox1.SelectionSt art == 0)
{
return '0';
}
else
{
return
this.richTextBo x1.Text[this.richTextBo x1.SelectionSta rt - 1];
}
}
private bool IsMatchKeyword( string text)
{
int i = 0;
for (; i < myWordList.Leng th; i++)
{
if (myWordList[i] == text)
{
break;
}
}
if (i < myWordList.Leng th)
return true;
else
return false;
}
private void ProcessCurrentW ord(int carsetIndex)
{
int wordstart = carsetIndex -1;
while ((wordstart - 1 >= 0) &&
(this.richTextB ox1.Text[wordstart - 1] != ' ' &&
this.richTextBo x1.Text[wordstart-1]!='\n'))
{
wordstart--;
}
int wordend = carsetIndex - 1;
while ((wordend + 1 < this.richTextBo x1.TextLength) &&
this.richTextBo x1.Text[wordend + 1] != ' ' &&
this.richTextBo x1.Text[wordend +1] != '\n')
{
wordend++;
}
string wordtext = this.richTextBo x1.Text.Substri ng(wordstart,
wordend - wordstart + 1);
this.richTextBo x1.SelectionSta rt = wordstart;
this.richTextBo x1.SelectionLen gth = wordend - wordstart + 1;
if (IsMatchKeyword (wordtext))
{
this.richTextBo x1.SelectionCol or = this.hilightCol or;
}
else
{
this.richTextBo x1.SelectionCol or =
this.richTextBo x1.ForeColor;
}
this.richTextBo x1.SelectionSta rt = carsetIndex;
this.richTextBo x1.SelectionLen gth = 0;
}
private void ProcessPreWord( int carsetIndex)
{
int wordend = carsetIndex -1;
while (wordend >= 0 && (this.richTextB ox1.Text[wordend] == ' '
|| this.richTextBo x1.Text[wordend] == '\n'))
{
wordend--;
}
int wordstart = wordend;
if (wordstart 0)
{
while ((wordstart - 1 >= 0) &&
this.richTextBo x1.Text[wordstart-1] != ' ' &&
this.richTextBo x1.Text[wordstart-1]!='\n')
{
wordstart--;
}
}
if (wordstart >= 0)
{
this.richTextBo x1.SelectionSta rt = wordstart;
this.richTextBo x1.SelectionLen gth = wordend - wordstart + 1;
string wordtext =
this.richTextBo x1.Text.Substri ng(wordstart, wordend - wordstart + 1);
if (IsMatchKeyword (wordtext))
{
this.richTextBo x1.SelectionCol or = this.hilightCol or;
}
else
{
this.richTextBo x1.SelectionCol or =
this.richTextBo x1.ForeColor;
}
this.richTextBo x1.SelectionSta rt = carsetIndex;
this.richTextBo x1.SelectionLen gth = 0;
}
}
private void ProcessNextWord (int carsetIndex)
{
int wordstart = carsetIndex -1;
while ((wordstart < this.richTextBo x1.TextLength) &&
(this.richTextB ox1.Text[wordstart] == ' ' ||
this.richTextBo x1.Text[wordstart]=='\n'))
{
wordstart++;
}
int wordend = wordstart;
if (wordend < this.richTextBo x1.TextLength - 1)
{
while (wordend + 1 < this.richTextBo x1.TextLength &&
this.richTextBo x1.Text[wordend +1]!= ' '&&
this.richTextBo x1.Text[wordend+1]!='\n')
{
wordend++;
}
}
if (wordstart < this.richTextBo x1.TextLength)
{
this.richTextBo x1.SelectionSta rt = wordstart;
this.richTextBo x1.SelectionLen gth = wordend - wordstart + 1;
string wordtext = this.richTextBo x1.Text.Substri ng
(wordstart,word end - wordstart +1);
if (IsMatchKeyword (wordtext))
{
this.richTextBo x1.SelectionCol or = this.hilightCol or;
}
else
{
this.richTextBo x1.SelectionCol or =
this.richTextBo x1.ForeColor;
}
this.richTextBo x1.SelectionSta rt = carsetIndex;
this.richTextBo x1.SelectionLen gth = 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.