Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 265
CJ7 Avatar asked Aug 06 '10 06:08

CJ7


People also ask

How do you unhighlight a text box?

If the highlighting that you are talking about is really that the text is being selected inside the textbox, you can modify it by changing the textbox . SelectionStart . Set it to zero and the cursor will be at the first position in the textbox. Set it equal to the textbox's length and the cursor will be at the end.

Which property is used to get the selected text in the textbox?

SelectedText Property (System.


2 Answers

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.

like image 193
fletcher Avatar answered Sep 18 '22 03:09

fletcher


The default behavior of a TextBox in Windows Forms is to highlight all of the text if it gets focused for the first time by tabbing into it, but not if it is clicked into. We can see this in Reflector by looking at the TextBox's OnGotFocus() override:

protected override void OnGotFocus(EventArgs e) {     base.OnGotFocus(e);     if (!this.selectionSet)     {         this.selectionSet = true;         if ((this.SelectionLength == 0) && (Control.MouseButtons == MouseButtons.None))         {             base.SelectAll();         }     } } 

It's that if statement that is causing the behavior that we don't like. Furthermore, to add insult to injury, the Text property's setter blindly resets that selectionSet variable whenever the text is re-assigned:

public override string Text {     get     {         return base.Text;     }     set     {         base.Text = value;         this.selectionSet = false;     } } 

So if you have a TextBox and tab into it, all the text will be selected. If you click into it, the highlight is removed, and if you re-tab into it, your caret position (and selection length of zero) is preserved. But if we programmatically set new Text, and tab into the TextBox again, then all of the text will be selected again.

If you are like me and find this behavior annoying and inconsistent, then there are two ways around this problem.

The first, and probably the easiest, is to simply trigger the setting of selectionSet by calling DeselectAll() on form Load() and whenever the Text changes:

protected override void OnLoad(EventArgs e) {     base.OnLoad(e);      this.textBox2.SelectionStart = this.textBox2.Text.Length;     this.textBox2.DeselectAll(); } 

(DeselectAll() just sets SelectionLength to zero. It's actually SelectionStart that flips the TextBox's selectionSet variable. In the above case, the call to DeselectAll() is not necessary since we are setting the start to the end of the text. But if we set it to any other position, like the start of the text, then calling it is a good idea.)

The more permanent way is to create our own TextBox with the desired behavior through inheritance:

public class NonSelectingTextBox : TextBox {     // Base class has a selectionSet property, but its private.     // We need to shadow with our own variable. If true, this means     // "don't mess with the selection, the user did it."     private bool selectionSet;      protected override void OnGotFocus(EventArgs e)     {         bool needToDeselect = false;          // We don't want to avoid calling the base implementation         // completely. We mirror the logic that we are trying to avoid;         // if the base implementation will select all of the text, we         // set a boolean.         if (!this.selectionSet)         {             this.selectionSet = true;              if ((this.SelectionLength == 0) &&                  (Control.MouseButtons == MouseButtons.None))             {                 needToDeselect = true;             }         }          // Call the base implementation         base.OnGotFocus(e);          // Did we notice that the text was selected automatically? Let's         // de-select it and put the caret at the end.         if (needToDeselect)         {             this.SelectionStart = this.Text.Length;             this.DeselectAll();         }     }      public override string Text     {         get         {             return base.Text;         }         set         {             base.Text = value;              // Update our copy of the variable since the             // base implementation will have flipped its back.             this.selectionSet = false;         }     } } 

You maybe tempted to just not call base.OnGotFocus(), but then we would lose useful functionality in the base Control class. And you might be tempted to not mess with the selectionSet nonsense at all and simply deselect the text every time in OnGotFocus(), but then we would lose the user's highlight if they tabbed out of the field and back.

Ugly? You betcha. But it is what it is.

like image 33
Nicholas Piasecki Avatar answered Sep 19 '22 03:09

Nicholas Piasecki