Ordering the Index of Controls on a Form

00shoe

Member
Joined
Oct 12, 2006
Messages
20
Programming Experience
3-5
Hey,

I’m using a form with 3 tabs. All 3 tabs have identical controls and properties on each tab.

And when I want to call a control for any tab I use the following bit of code.

Me.tabcontrol.selectedtab.Controls(0)

However this does not work as for example when I use this code on the first tab it will call a button, and when I use this on the second tab it would call a textbox.

My question: Is it possible to “order” the index of the control on the form.
e.g.
Me.tabcontrol.selectedtab.Controls(0) = Button
Me.tabcontrol.selectedtab.Controls(1) = Textbox
Me.tabcontrol.selectedtab.Controls(2) = Another Textbox
Me.tabcontrol.selectedtab.Controls(3) = Label
Etc…

Thank-you
 
What you should do is create a UserControl that contains all the individual controls you need and then add a single instance to each TabPage. Your UserControl can provide its own properties and methods so it is never necessary to access the constituent controls directly, e.g.
VB.NET:
Public Property BoxText() As String
    Get
        Return Me.TextBox1.Text
    End Get
    Set (ByVal value As String)
        Me.TextBox1.Text = value
    End Set
End Property
or else it can provide direct access to each control through a ReadOnly property, e.g.
VB.NET:
Public ReadOnly Property TextBox() As TextBox
    Get
        Return Me.TextBox1
    End Get
End Property
If all you want to be able to do is access the control's contents then I'd suggest the first option.

If you're determined to use the method you currently are then you should be aware that the controls are retuned from the collection in the same order they were added. You can expand the designer code and simply drag the lines that add the controls to the TabPages so that they are in the same order for each TabPage. That's still a bad idea though because you aren't guaranteed that the designer code won't be changed at some point. I strongly suggest using the UserControl approach. It is much, much more "correct".
 
Thank-you!, I've been use the UserControl way and it so much easier, especially for maintainability.

Many Thanks!
 
Back
Top