First thing the Click
event of the TabPage
control fires when the user clicks inside the TabPage
not on the header so your SelectedIndexChanged
event is the one you want to use.
I just tested code very similiar to yours:
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabControl1.SelectedTab == tabPage2)
{
textBox4.Focus();
}
}
And it worked fine.
Is the password textbox not enabled or something like that?
If you try to call Focus()
on a different control does that also not work?
If you set a breakpoint inside the SelectedIndexChanged
code does it get hit?
Update: Interesting. If the breakpoint isn't getting hit (before the if
) I would double check that your eventhandler is properly attached. Look in your designer.cs for something like:
this.tabControl1.SelectedIndexChanged += new System.EventHandler(this.tabControl1_SelectedIndexChanged);
Update: I put my working example at http://www.ccswe.com/temp/SO_TextBoxFocus.zip maybe looking at it will help you figure out where the issue is.
Update: The easier way to attach an event handler to a control on your form:
1: Select the Control
to want to attach an event handler to and then click the Events
icon (lightning bolt) in the Properties
window.
2: Find the event you want to attach to and double click to the right.
3: A code stub will be automatically generated for you and the event will be attached in the designer.
If you look at the properties window again you'll now see the name of the method that was generated.