Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

JvCoach23

VB.Net newbie

MS Sql Vet

Posted

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();

Posted

//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

Software bugs are impossible to detect by anybody except the end user.
Posted

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

  • Leaders
Posted

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?

[sIGPIC]e[/sIGPIC]
Posted

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();

JvCoach23

VB.Net newbie

MS Sql Vet

  • Leaders
Posted

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.

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...