jvcoach23 Posted March 1, 2005 Posted March 1, 2005 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 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 Quote JvCoach23 VB.Net newbie MS Sql Vet
Diesel Posted March 1, 2005 Posted March 1, 2005 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(); Quote
AlexCode Posted March 1, 2005 Posted March 1, 2005 //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 Quote Software bugs are impossible to detect by anybody except the end user.
Diesel Posted March 1, 2005 Posted March 1, 2005 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 Quote
Leaders snarfblam Posted March 2, 2005 Leaders Posted March 2, 2005 This code makes the most sense to me: 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? Quote [sIGPIC]e[/sIGPIC]
jvcoach23 Posted March 2, 2005 Author Posted March 2, 2005 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 tabControl1.TabPages[0].Controls[tabControl1.TabPages[0].Controls.IndexOf(textBox2)].Focus(); Quote JvCoach23 VB.Net newbie MS Sql Vet
Leaders snarfblam Posted March 3, 2005 Leaders Posted March 3, 2005 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: 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. Quote [sIGPIC]e[/sIGPIC]
pendragon Posted March 3, 2005 Posted March 3, 2005 Another way to do it is tabControl1.SelectedTab = tabControl1.TabPages[0]; [/Code] Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.