473,575 Members | 3,439 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_Activat e(object sender, EventArgs e)
{
if (txtOne.Text != "")
{
this.txtTwo.Foc us();
}
}

private void InitializeCompo nent()
{
this.txtOne = new System.Windows. Forms.TextBox() ;
this.txtTwo = new System.Windows. Forms.TextBox() ;
this.SuspendLay out();
//
// txtOne
//
this.txtOne.Loc ation = new System.Drawing. Point(12, 70);
this.txtOne.Nam e = "txtOne";
this.txtOne.Siz e = new System.Drawing. Size(100, 20);
this.txtOne.Tab Index = 0;
this.txtOne.Tex t = "I\'m full!";
//
// txtTwo
//
this.txtTwo.Loc ation = new System.Drawing. Point(180, 70);
this.txtTwo.Nam e = "txtTwo";
this.txtTwo.Siz e = new System.Drawing. Size(100, 20);
this.txtTwo.Tab Index = 1;
//
// frmMain
//
this.AutoScaleD imensions = new System.Drawing. SizeF(6F,
13F);
this.AutoScaleM ode =
System.Windows. Forms.AutoScale Mode.Font;
this.ClientSize = new System.Drawing. Size(292, 273);
this.Controls.A dd(this.txtTwo) ;
this.Controls.A dd(this.txtOne) ;
this.Name = "frmMain";
this.Text = "Form1";
this.Load += new
System.EventHan dler(this.frmMa in_Activate);
this.ResumeLayo ut(false);
this.PerformLay out();

}

Thanks,
Christian
Jul 13 '08 #1
3 1970
"Christian Blackburn" <ch************ *****@yahoo.com wrote in message
news:0b******** *************** ***********@26g 2000hsk.googleg roups.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_Activat e(object sender, EventArgs e)
{
if (txtOne.Text != "")
{
this.txtTwo.Foc us();
}
}
[...]
this.Load += new
System.EventHan dler(this.frmMa in_Activate);
Look at the last line that I quoted. You are connecting the routine that you
named "frmMain_Activa te" 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-
quitaestoparaco ntes...@poblaci on.orgwrote:
"Christian Blackburn" <christian.Blac kb...@yahoo.com wrote in message

news:0b******** *************** ***********@26g 2000hsk.googleg roups.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_Activat e(object sender, EventArgs e)
* * * *{
* * * * * *if (txtOne.Text != "")
* * * * * *{
* * * * * * * *this.txtTwo.Fo cus();
* * * * * *}
* * * *}
[...]
* * * * * *this.Load += new
System.EventHan dler(this.frmMa in_Activate);

Look at the last line that I quoted. You are connecting the routine that you
named "frmMain_Activa te" 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.com wrote in message
news:a7******** *************** ***********@w7g 2000hsa.googleg roups.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
2242
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 the focus to it (only if the checkbox.checked=true). So on the checkbox Leave-event i do the following: if checkbox.checked=true then...
0
1479
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 the focus method. The problem is: the focus is lost almost inmediately. I can't type in anything. See the code below.
2
1543
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 can't change it ... or can I ? thanks C
4
3104
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 UserControls into the PlaceHolder's child control collection depending upon which of the three radio buttons is selected. Each of the three UserControls have...
4
3013
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, as needed, I'm having trouble wiring up its click event procedure. The problem is that when I go to subscribe the ImageButton to the delegate, the...
4
2229
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 type=\"text/javascript\">" + oMenu.BuildMenu(true, true, true) + "</script>"); But the script is added to the end of the page, but I need it to be...
2
1701
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 = LoadControl("../UserControls/ISJob/uctlJobGeneral.ascx");
8
2803
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. How can I get the height of the control (in code) after it grows? I am building a windows form dynamically, creating controls and placing them on...
1
1542
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 control on this page the focus is shifted to the window element instead of the control itself. On every other text-box control on the page the focus is...
0
7775
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8120
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8281
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8143
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5664
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5338
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3778
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3797
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1382
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.