views:

785

answers:

1

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?

+6  A: 

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

Jeff Yates
Thanks! Everything is now working as I expected.
axk
Most welcome. :)
Jeff Yates
Umm, I don't see this property in a WPF TextBox
Dmitri Nesteruk
I'm not surprised, WPF does things differently to WinForms.
Jeff Yates