I’d like give focus to a textBox after a Tab has been selected but no matter what I try it doesn’t work. I’ve looked at similar questions here but they don’t get me the results I need. Here is what Ive tried.
private void tabBDERip_Click(object sender, EventArgs e)
{
textBoxPassword.Focus();
}
and
private void tabAll_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabAll.SelectedTab == tabBDERip)
{
textBoxPassword.Focus();
}
}
Can someone please tell me what I’m doing wrong?
Thanks
First thing the
Clickevent of theTabPagecontrol fires when the user clicks inside theTabPagenot on the header so yourSelectedIndexChangedevent is the one you want to use.I just tested code very similiar to yours:
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
SelectedIndexChangedcode 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: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
Controlto want to attach an event handler to and then click theEventsicon (lightning bolt) in thePropertieswindow.alt text http://www.ccswe.com/temp/Attach_EventHandler_1.png
2: Find the event you want to attach to and double click to the right.
alt text http://www.ccswe.com/temp/Attach_EventHandler_2.png
3: A code stub will be automatically generated for you and the event will be attached in the designer.
alt text http://www.ccswe.com/temp/Attach_EventHandler_3.png
If you look at the properties window again you’ll now see the name of the method that was generated.