Reference Text controls in other tabs

sskelton

Member
Joined
Sep 1, 2011
Messages
7
Programming Experience
10+
i am building a tab control (using VS 2010 and 4.0 framework). on Tab 0, this works as expected:

Me.ddUE_B1.SelectedValue = Me.B1.Text <-- changes the combo box selection to what is in the label.

on the other tabs this does not work. when I do a messagebox to show, say, Me.Ea1.Text on Tab 1, nothing is returned.

Is there an issue with referencing controls on tabs that I am missing? In general, I will need to set combo box's value to what is in the database when the form loads.

Thanks in advance for your help!
 
Whether or not controls are placed on tabs, the variables used to reference them are still members of the form so, if your code is in the form, they are always accessed exactly the same way, with no care for whether they are in a Panel, GroupBox or TabPage and at what level of nesting. Your code is never "on a tab". Your code is always in the form. Try this for a test:

1. Create a new WinForms application project.
2. Add a TabControl to the form.
3. Add a Button and a TextBox to the first TabPage.
4. Set the Text of the TextBox to "TextBox1 on TabPage1".
5. Add a Button and a TextBox to the second TabPage.
6. Set the Text of the TextBox to "TextBox2 on TabPage2".
7. Add the following code:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    MessageBox.Show(Me.TextBox1.Text)
    MessageBox.Show(Me.TextBox2.Text)
End Sub

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    MessageBox.Show(Me.TextBox1.Text)
    MessageBox.Show(Me.TextBox2.Text)
End Sub
8. Run the project and click both Buttons.

You'll notice that, in both cases, the correct text is displayed for both TextBoxes. The code makes no reference to the TabControl or TabPages.

If you're seeing some other behaviour in your app then either something is corrupt or, more likely, you have done something wrong. Without a more complete picture of what you're doing, we couldn't say what you're doing wrong.
 
Actually, there is a 'gotcha' with reading textboxes on tab controls. it seems that the textbox values only "bind" when the tab itself is selected. so, if i try to read values on Tab 0 in the form load event, that works as Tab 0 is displayed as a default. Tab 1 hasn't been 'drawn' yet so no value is assigned. What needs to happen is read and assign control values in the Tab Paint event.
 
The TabControl will avoid actually creating child control handles until the parent TabPage is displayed. I don't think data-binding occurs until the handle is created. Try calling CreateControl on your TabPage in the form's Load event handler. The fact that you're using data-binding would fall under the category of "a more complete picture".
 
Back
Top