Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

textBox.selectAll()

Status
Not open for further replies.

Ginka

Programmer
Mar 18, 2005
54
MX
I want to select all the text when the control gets the focus.

I mean this
Code:
private void textBoxDesc_Enter(object sender, EventArgs e)
        {
            textBoxDesc.SelectAll();
        }

But the text doesn't get selected, why?

 
textBoxDesc.Text.SelectAll();
textBoxDesc.Focus();
textBoxDesc.Activate();

A combination of those 3 should work for you.
 
but Activate() is not a member of TextBox Class
 
This is VB.Net, but it will take no more than 10 seconds to convert and seems to do what you want:

Code:
  [Blue]Private[/Blue] textbox1focused [Blue]As[/Blue] [Blue]Boolean[/Blue] = [Blue]False[/Blue]

  [Blue]Private[/Blue] [Blue]Sub[/Blue] TextBox1_GotFocus([Blue]ByVal[/Blue] sender [Blue]As[/Blue] Object, [Blue]ByVal[/Blue] e [Blue]As[/Blue] System.EventArgs) [Blue]Handles[/Blue] TextBox1.GotFocus

    TextBox1.SelectAll()

  [Blue]End[/Blue] [Blue]Sub[/Blue]

  [Blue]Private[/Blue] [Blue]Sub[/Blue] TextBox1_Click([Blue]ByVal[/Blue] sender [Blue]As[/Blue] Object, [Blue]ByVal[/Blue] e [Blue]As[/Blue] System.EventArgs) [Blue]Handles[/Blue] TextBox1.Click

    [Blue]If[/Blue] Not textbox1focused [Blue]Then[/Blue]
      textbox1focused = [Blue]True[/Blue]
      TextBox1.SelectAll()
    [Blue]End[/Blue] [Blue]If[/Blue]

  [Blue]End[/Blue] [Blue]Sub[/Blue]

  [Blue]Private[/Blue] [Blue]Sub[/Blue] TextBox1_LostFocus([Blue]ByVal[/Blue] sender [Blue]As[/Blue] Object, [Blue]ByVal[/Blue] e [Blue]As[/Blue] System.EventArgs) [Blue]Handles[/Blue] TextBox1.LostFocus

    textbox1focused = [Blue]False[/Blue]

  [Blue]End[/Blue] [Blue]Sub[/Blue]

Hope this helps.

[vampire][bat]
 
this works when a user tabs into the box
Code:
texbox.Focus();

Age is a consequence of experience
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top