471,337 Members | 1,338 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Dynamically placing focus on a specific control at startup

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
3 1902
"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
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
"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.

Similar topics

reply views Thread by Carlos Lozano | last post: by
2 posts views Thread by ilPostino | last post: by
4 posts views Thread by Harry | last post: by
4 posts views Thread by Jaime Stuardo | last post: by
2 posts views Thread by Vivek Sharma | last post: by
1 post views Thread by orico | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.