textbox focus

  • Thread starter =?ISO-8859-1?Q?S=E9rgio_Almeida?=
  • Start date
?

=?ISO-8859-1?Q?S=E9rgio_Almeida?=

greetings

i have a form with a textbox and one button and i want the textbox to
have the focus when the form is loading. for this i put the following
code on the form's load event : this.textbox1.focus();

but this does not work. the button on my form gets the focus even when I
use the code above (by the way, the code above returns false). but if I
change the button's visible property to false, the code works and I get
the textbox focused on form load. How can I prevent this from happening?
any ideas will be appreciated.

TIA

Almeida
 
G

Guest

I believe that the TabIndex sets which control gets intial focus... Try changing the TabIndex as below...

//
// button1
//
this.button1.Location = new System.Drawing.Point(96, 16);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(104, 64);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Sérgio,

If you don't want to change the tab order what you need to do is to set
Form's ActiveControl property to the control you want to have the focus
initially, insted of calling control's Focus method. The reason being is
that Focus method checks if the control is visible and if it is not it
doesn't do anything. Apparently in the constructor or Load event nothing's
visible.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top