Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I highlight a selection made programmatically in a Winforms TextBox

I've not gone into much research here, but the intuitive thing is not working:

private void SerachButton1_Click(object sender, EventArgs e)
{
   String serchTerm = searchTerm1.Text;
   String text = usualTextBox.Text;


   Int32 index = text.IndexOf(serchTerm);

   if (index >= 0)
   {
      usualTextBox.Select(index, serchTerm.Length);
   }
}

SelectedText, SelectionLength and SelectionStart properties are as I expect them after Select is called but there's no visible selection.

What am I doing wrong here?

Edit: I've also tried RichTextBox. When I set background and text colors for the selection it shows up, but it won't unselect automatically when you manually select another part of text or just click on a position in text. Are these two types of selection inherently different and if you select programmatically you have to also deselect programmatically?

like image 895
axk Avatar asked Nov 02 '08 18:11

axk


People also ask

How do I select all in a text box?

Now, press and hold the CTRL key and click the next one. Repeat the same steps to go over all of the text boxes in your document. There you have it! That's how you use the CTRL key and your mouse to select multiple text boxes in Word.


1 Answers

You need to set usualTextBox.HideSelection to false so that the selection remains visible when the focus is not in the TextBox.

like image 105
Jeff Yates Avatar answered Oct 26 '22 22:10

Jeff Yates