Highlight Textbox Text

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to "highlight" text in a TextBox control during the "Enter" event as follows

private void txtPriorPlantTotal_Enter(object sender, System.EventArgs e

this.txtPriorPlantTotal.SelectionStart = 0
this.txtPriorPlantTotal.SelectionLength = this.txtPriorPlantTotal.Text.Length


However, when I run the program and click on the textbox none of the text is physicaly highlighted. SelectedText shows I have selected all the text in the textbox but nothing is highlighted. I tried a few other textboxes just to make sure it wasn't the control

What gives

C# with framework 1.1
 
* "=?Utf-8?B?Q2hyaXMgQ2xpbmU=?= said:
I am trying to "highlight" text in a TextBox control during the "Enter" event as follows:

private void txtPriorPlantTotal_Enter(object sender, System.EventArgs e)
{
this.txtPriorPlantTotal.SelectionStart = 0;
this.txtPriorPlantTotal.SelectionLength = this.txtPriorPlantTotal.Text.Length;
}

However, when I run the program and click on the textbox none of the
text is physicaly highlighted. SelectedText shows I have selected all
the text in the textbox but nothing is highlighted. I tried a few other
textboxes just to make sure it wasn't the control.

Try to call the control's 'SelectAll' method. If this doesn't work, add
the code to the control's 'GotFocus' event handler.
 
I have tried using the SelectAll and it still doesn't "highlight" any text
I also tried using the GotFocus event and still nothing. I can see the event being fired and the SelectedText show I have the text but still nothing is highlighted
 
Use TextLength (is one word) instead of Text.Length (note the missing dot)
and it should work. In your example:

Change this line ...

this.txtPriorPlantTotal.SelectionLength =
this.txtPriorPlantTotal.Text.Length;

to ...

this.txtPriorPlantTotal.SelectionLength =
this.txtPriorPlantTotal.TextLength;


Ciao

--

Best Regards - Behram
Chris Cline said:
I have tried using the SelectAll and it still doesn't "highlight" any text.
I also tried using the GotFocus event and still nothing. I can see the
event being fired and the SelectedText show I have the text but still
nothing is highlighted.
 
Back
Top