473,408 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 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 1965
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: kea | last post by:
I have a checkbox and a textbox in a continuous taborder on a form along with a bunch of other controls. The textbox is disabled. When I leave the checkbox I want to enable the textbox and move...
0
by: Carlos Lozano | last post by:
Hello everybody, I Have a piece of code for a Pocket PC that has a disabled and hidden textbox control at startup. It is enabled and set visible programatically. The cursor is captured calling...
2
by: ilPostino | last post by:
ok, so I have my asp.net application. I want a single page which can load asp user controls and at runtime. How can this be done? I don't want to drag the control onto the page because then I...
4
by: Harry | last post by:
Hello, I have a page with a RadioButtonList and a PlaceHolder control. The RadioButtonList's AutoPostBack attribute is set to TRUE and its SelectedIndexChanged event loads one of three...
4
by: Jordan | last post by:
I need to dynamically add an ImageButton control to a user control and and do some server-side processing when the user clicks it. While I the ImageButton is added to the user control at runtime,...
4
by: Jaime Stuardo | last post by:
Hi all... I need to add some JavaScript code that is dynamically generated to some point of the page. Currently I'm using ClientScript.RegisterStartupScript(GetType(), "menu", "<script...
2
by: Vivek Sharma | last post by:
Hi There, I have a situation where I wish to load the controls dynamically on the basis of user role. Hence, I am using this code. if (UserRole == "IS Administrator") { Control UC1 =...
8
by: BillE | last post by:
When I create a control dynamically and it grows according to the content, the control Height property still returns the original default control height instead of the height after expanding. ...
1
by: orico | last post by:
Hello all, I have a few '<input type="text" />' controls on my ASP.NET 2.0 generated page. Every text-box control has it's specific tab-index. When I try to move focus to this specific text-box...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.