Why is text in TextBox highlighted (selected) when form is displayed?

CJ7 picture CJ7 · Aug 6, 2010 · Viewed 82.2k times · Source

I have a form containing a TextBox in C# which I set to a string as follows:

textBox.Text = str;

When the form is displayed, why does the text in the texbox appear highlighted/selected?

Answer

fletcher picture fletcher · Aug 6, 2010

The text box has a TabIndex of 0 and TabStop set to true. This means that the control will be given focus when the form is displayed.

You can either give another control the 0 TabIndex (if there is one) and give the text box a different tab index (>0), or set TabStop to false for the text box to stop this from happening.