Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

set focus to textbox within a tab 2

Status
Not open for further replies.

ksbigfoot

Programmer
Apr 15, 2002
856
CA
I am writing a windows application
I am popping up a new form that has a tab control with 2 tabs in it.
I am trying to have the first tab displayed and focus set on a text box within that tab.
I thought I would only have to say
txtBox1.Focus() on the form load event.

Could someone point me in the right direction?
Thanks
 
Use this to activate the first tab page:

TabControl1.SelectedTab = TabControl1.TabPages(0)



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
This is normally what I use, though jebenson is correct.

Code:
tabControl1.SelectedIndex = TabPage.GetTabPageOfComponent(txtBox1).TabIndex

And then...

Code:
txtBox1.Focus()

I hope this helps.




Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.
 
Howdy jebenson & Ron,

I tried both your ways and my tab is highlighted, but the focus wasn't given to the textbox field.

Should I be doing this in the on form load event?
Here is the code I have
Code:
   Private Sub frmProcessEmployee_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TabControl1.SelectedTab = TabControl1.TabPages(0)
        'txtBox1.Focus()
        TabControl1.SelectedIndex = TabPage.GetTabPageOfComponent(txtBox1).TabIndex
        txtFirstName.Focus()
    End Sub
Thanks for the help
 
Is txtBox1 and txtFirstName on the same page?

If not, there's your problem.

I don't think it matters where you put it. I use mine behind button events and it immediately draws focus.

Sorry I couldn't help more.

Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.
 
Howdy Ron,
I changed the name of my textbox and forgot to change it in the focus code. It still didn't work when I used the code in the On_Load event. But from your tip about putting it behind button events. I moved the code to the place where I call the form to popup. It works perfectly!!

Thanks again for the help.
I gave you both a star as both code works.

Thanks,
ksbigfoot
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top