Elad Lachmi
Elad Lachmi

Reputation: 10561

Deselect text in RichTextBox

I have a RichTextBox with a search box under it. I use the following code for the search functionality:

TabPage activePage = tabs.SelectedTab;
            RichTextBox xmlBox = activePage.Controls.Find("xmlBox", true).Single() as RichTextBox;

xmlBox.DeselectAll();

int index = 0;
int len = xmlBox.TextLength;
int lastIndex = xmlBox.Text.LastIndexOf(tbSearch.Text);

while (index < lastIndex)
{
     xmlBox.Find(tbSearch.Text, index, len, RichTextBoxFinds.WholeWord);
     xmlBox.SelectionBackColor = Color.Yellow;
     index = xmlBox.Text.IndexOf(tbSearch.Text, index) + 1;
}

What I want is that lets say a user types the word User. When he types the U I want all the Us to be highlighted, etc. and then if he deletes the r I want only Use to be highlighted. I was thinking that DeselectAll() would do the trick, but this isn`t working. Any other way to do this?

Upvotes: 1

Views: 9993

Answers (1)

Idle_Mind
Idle_Mind

Reputation: 39122

DeselectAll() will simply unselect any current selection. Your code actually changed the BackColor() of previous text so you'd have to undo this...possibly by selecting everything and resetting it to the default color before again highlighting the new search value.

Upvotes: 6

Related Questions