By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,421 Members | 1,386 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,421 IT Pros & Developers. It's quick & easy.

Dynamically placing focus on a specific control at startup

P: n/a
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
Jul 13 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Christian Blackburn" <ch*****************@yahoo.comwrote in message
news:0b**********************************@26g2000h sk.googlegroups.com...
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.

Jul 14 '08 #2

P: n/a
On Jul 13, 11:43*pm, "Alberto Poblacion" <earthling-
quitaestoparacontes...@poblacion.orgwrote:
"Christian Blackburn" <christian.Blackb...@yahoo.comwrote in message

news:0b**********************************@26g2000h sk.googlegroups.com...
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
Jul 14 '08 #3

P: n/a
"Christian Blackburn" <ch*****************@yahoo.comwrote in message
news:a7**********************************@w7g2000h sa.googlegroups.com...
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.

Jul 14 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.