Dynamically placing focus on a specific control at startup

  • Thread starter Christian Blackburn
  • Start date
C

Christian Blackburn

Hi Gang,

I have two text boxes on a form and if the first text box is
populated, I want to place focus on the second text box at startup.
However, this never seems to work. What am I doing wrong here?

private void frmMain_Activate(object sender, EventArgs e)
{
if (txtOne.Text != "")
{
this.txtTwo.Focus();
}
}

private void InitializeComponent()
{
this.txtOne = new System.Windows.Forms.TextBox();
this.txtTwo = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// txtOne
//
this.txtOne.Location = new System.Drawing.Point(12, 70);
this.txtOne.Name = "txtOne";
this.txtOne.Size = new System.Drawing.Size(100, 20);
this.txtOne.TabIndex = 0;
this.txtOne.Text = "I\'m full!";
//
// txtTwo
//
this.txtTwo.Location = new System.Drawing.Point(180, 70);
this.txtTwo.Name = "txtTwo";
this.txtTwo.Size = new System.Drawing.Size(100, 20);
this.txtTwo.TabIndex = 1;
//
// frmMain
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F,
13F);
this.AutoScaleMode =
System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.txtTwo);
this.Controls.Add(this.txtOne);
this.Name = "frmMain";
this.Text = "Form1";
this.Load += new
System.EventHandler(this.frmMain_Activate);
this.ResumeLayout(false);
this.PerformLayout();

}

Thanks,
Christian
 
A

Alberto Poblacion

Christian Blackburn said:
I have two text boxes on a form and if the first text box is
populated, I want to place focus on the second text box at startup.
However, this never seems to work. What am I doing wrong here?

private void frmMain_Activate(object sender, EventArgs e)
{
if (txtOne.Text != "")
{
this.txtTwo.Focus();
}
}
[...]
this.Load += new
System.EventHandler(this.frmMain_Activate);

Look at the last line that I quoted. You are connecting the routine that you
named "frmMain_Activate" to the LOAD event of the form (instead of Activate,
as the name seems to imply). At the time of the Load event the form is not
yet visible, so the Focus() method doesn't work.
 
C

Christian Blackburn

I have two text boxes on a form and if the first text box is
populated, I want to place focus on the second text box at startup.
However, this never seems to work.  What am I doing wrong here?
       private void frmMain_Activate(object sender, EventArgs e)
       {
           if (txtOne.Text != "")
           {
               this.txtTwo.Focus();
           }
       }
[...]
           this.Load += new
System.EventHandler(this.frmMain_Activate);

Look at the last line that I quoted. You are connecting the routine that you
named "frmMain_Activate" to the LOAD event of the form (instead of Activate,
as the name seems to imply). At the time of the Load event the form is not
yet visible, so the Focus() method doesn't work.

Hi Alberto,

I tried to this.Active += new; and that didn't work, so I used
this.Load which I wasn't sure whether that was the name of the event
or the action of loading that event handler. This is the compiler
error I get when I try using activate:
Cannot assign to 'Activate' because it is a 'method group'

Do you think I am declaring my event in the wrong place?

Thanks,
Christian
 
A

Alberto Poblacion

Christian Blackburn said:
I tried to this.Active += new; and that didn't work,

Should be "Activated", not "Active".

Anyway, when in doubt, you can use the designer in Visual Studio to
connect the event for you: Click on the form in design view, and then go to
the Properties window and locate an icon at the top of this window that
looks like a lightning bolt. When you press it, the properties window
displays the events instead of the properties. Here you can remove an event
that you have connected and add a new event handler, or connect an existing
one, to the event of your choice.
 

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