Visual Studio Development Bookmark and Share   
 index > Visual C# Express Edition > Get cursor in a text box so user doesn't have to click it to enter text
 

Get cursor in a text box so user doesn't have to click it to enter text

I have a little panel app I'm creating in C#, that has a text box.
When I run it now, I click a button to start, then I have to click the text box to be able to enter the text into it.
It works just fine.
But,  I would like to click the button then have the cursor show us in the panel so the user doesn't have to click it.

Thanks for your help.
uzziah0  Thursday, October 15, 2009 5:41 PM
In the button's click handler, just set the focus to the text box:

void button1_OnClick(object sender, EventArgs e)
{
     this.textBox1.Focus();
}

This was answered preivously .  The process is the same, except that you'll want to do this in the button's event, instead of on Form_Load.
Reed Copsey, Jr. - http://reedcopsey.com
Reed Copsey, Jr.  Thursday, October 15, 2009 6:08 PM
In the button's click handler, just set the focus to the text box:

void button1_OnClick(object sender, EventArgs e)
{
     this.textBox1.Focus();
}

This was answered preivously .  The process is the same, except that you'll want to do this in the button's event, instead of on Form_Load.
Reed Copsey, Jr. - http://reedcopsey.com
Reed Copsey, Jr.  Thursday, October 15, 2009 6:08 PM
What is "this"?

I've seen this in other examples but I'm not sure what it is.

I'm still not sure what "this" is, but I tried this

  textBox3.Focus();

and it works.

Thanks


uzziah0  Thursday, October 15, 2009 6:09 PM
When you're working within a class's method, you can use "this" to refer to a member of the class.  It's entirely optional, but considered good practice by many, since it makes it very explicit.

The only time it's required is if you have a local variable or argument to a method with the same name as a class variable, otherwise, it's optional:

class MyClass
{
    private int number;

    public void Foo()
    {
        number = 1; // this sets the class's private number variable, since there's no other "number" variable here...
    }

    public void RunCalculation(int number) // Note the same name used here as above
    {
        // .. do some work

        // This will change the value of the argument passed in, but leave the number declared in the class alone
        number = 3;

        // this changes the class's private number variable directly
        this.number = 4;
    }

     public void DoWork()
     {
         int number = 5; // this sets up a local variable, but leaves the class's variable alone

         this.number = 2; // this sets the class's member explicitly
     }
}

When you're working in a form, the designer sets up variables for you at the class (form) level.  In your case, your text box is named "textBox3".

If you are in a method, and you do:

   textBox3.Focus();

It finds that variable, because there isn't anything named that in the current scope.  It will behave exactly the same as typing:

   this.textBox3.Focus();



Reed Copsey, Jr. - http://reedcopsey.com
Reed Copsey, Jr.  Thursday, October 15, 2009 6:46 PM

You can use google to search for other answers

Custom Search

More Threads

• ListBox
• Windows foms, moving a picturebox
• What is the proper method for renaming a resource?
• My dataGridView doesn't always register a click event?
• Disable (or Override) Javascript in WebBrowser control
• ListView Columns Issue
• code text editor
• can't connetc to sqo 2005 express from C#,C+ express
• RE: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
• Getting Setting Values