Tab Control and setting the focus

jvcoach23

Centurion
Joined
May 22, 2003
Messages
192
Location
Saybrook, IL
I'm working with a windows form and using the tab control. Lets say I'm on the second tab.. called tab2. In just learning and playing around.. I'd like to have a button on tab2, called button1 that when pressed, will shift the focus to tab1, textbox1. I've tried something like this

Visual Basic:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.textbox1.Focus()
    End Sub

this didn't work.. I've tried searching around but must be using the wrong key words.. anyone have the answer for me..

thanks
shannon
 
Hi Shannon

How about something like this

// 0 being the index of the tab page
tabControl1.SelectedIndex = 0;

//0 = tab page, 1 = index of control
tabControl1.TabPages[0].Controls[1].Focus();
 
Diesel said:
//0 = tab page, 1 = index of control
tabControl1.TabPages[0].Controls[1].Focus();

Just a short note on this line you posted Diesel...
The index of the controls on the Controls collection of a container isn't constant, so, if this is the solution, you must use a For Each or a simple For loop, to iterate thru all the items...

Alex :p
 
The index of a control is dependent on the order in which they are placed upon the owner control.

If you know the index of the control, thats the best.
If you know the name of the control, then you can...

tabControl1.TabPages[0].Controls[tabControl1.TabPages[0].Controls.IndexOf(textBox2)].Focus();

if not, then yes, iterate through the controls
 
This code makes the most sense to me:

Visual Basic:
tabControl1.SelectedIndex = 0
textBox2.Focus()

If you have a reference to the control (in this case the variable named textbox2), you don't need to do a for each to find the index, and you certianly dont need to use the reference of the control to find the index which you can then use in the controlcollection to... find the reference. Kind of redundant. No?
 
want to make sure I'm understanding this. i have the tabcontrol.selectedindex=0 thing working.. just want to make sure I'm not going to get into a pickle. The index for that tab control.. it can't change by itself right.. i'd have to change that or do put some code in. If i were to leave it as tabcontrol1.selectedindex=0, unless I do something that will stay the same. is that the right way to do it.. or should I do it like was described above

Visual Basic:
tabControl1.TabPages[0].Controls[tabControl1.TabPages[0].Controls.IndexOf(textBox2)].Focus();
 
The indecies of the tab pages are not going to change. I don't know if it is good practice to use tabControl1.SelectedIndex = 0, but I know it works.

Also, this:
Visual Basic:
tabControl1.TabPages[0].Controls[tabControl1.TabPages[0].Controls.IndexOf(textBox2)].Focus();
will work... but what this code if very redundant. it calls many functions to find the reference (address if you prefer) of textbox2, and does so by using a reference to textbox2, then brings focus to the control. My recommendation: don't use that code. It is not any safer than txtBox2.Focus(). When you want to select a control within a tabpage, set the selected tabpage (tabControl1.SelectedIndex = 0) and then set the focused control (txtBox2.Focus(); ). Thats it.
 
Back
Top