Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Iterating Control collection within a control - How is this done? 1

Status
Not open for further replies.

Kieran777

Programmer
Apr 23, 2003
26
AU
'My Base form (which I inherit in new update forms) has a toolbar which has buttons for Add, Edit, Delete etc. This form always has a tabcontrol and at least 1 tabpage. I have code (see below) that iterates through the controls on this forms Tabcontrols Tabpage (first one) and sets the control properties (for textbox and combobox) as required (this works fine). My problem is that I may have a Tabcontrol within this forms Tabcontrols 1st Tabpage, which itself has a number of Tabpages. I want to identify these Tabcontrols and iterate though their Tabpages looking for textbox and combobox controls so I can set their .text and .enable properties as required.

'Code assumes you have a form with a tabcontrol named 'TabCtrl1' and optional to see results need a text and/or combo box on the TabControls 1st TabPage.

Dim i As Integer
For i = 0 To Me.TabCtrl1.TabPages(0).Controls.Count - 1
If Me.TabControl1.TabPages(0).Controls(i).GetType() Is GetType(System.Windows.Forms.TextBox) Then
Me.TabCtrl1.TabPages(0).Controls(i).Text = ""
Me.TabCtrl1.TabPages(0).Controls(i).Enabled = True
Me.TabCtrl1.TabPages(0).Controls(i).ForeColor = color.blue
Me.TabCtrl1.TabPages(0).Controls(i).BackColor = color.yellow
ElseIf Me.TabCntrl1.TabPages(0).Controls(i).GetType() Is GetType(System.Windows.Forms.ComboBox) Then
Me.TabCtrl1.TabPages(0).Controls(i).Text = "NA"
Me.TabCtrl1.TabPages(0).Controls(i).Enabled = True
Me.TabCtrl1.TabPages(0).Controls(i).ForeColor = color.blue
Me.TabCtrl1.TabPages(0).Controls(i).BackColor = color.yellow
ElseIf Me.TabCtrl1.TabPages(0).Controls(i).GetType() Is GetType(System.Windows.Forms.TabControl) Then
' ...want code here to iterate through all the tab pages on this tabcontrol and set the textbox controls and combobox controls .text property and .enables property.
End If


Any Help appreciated.

Cheers
Kieran
 
You could set up a loop within this elseif statement to loop though all controls within this tab.

Something like

Code:
...ElseIf Me.TabCtrl1.TabPages(0).Controls(i).GetType() Is GetType(System.Windows.Forms.TabControl) Then

Dim c As Control
        Dim t As TabControl
        Dim p As TabPage

        For Each p In t.TabPages
            For Each c In p.Controls
                If c Is GetType(System.Windows.Forms.TextBox) Then
                    c.Text = ""
                    c.Enabled = True
                    c.ForeColor = Color.Blue
                    c.BackColor = Color.Yellow
                ElseIf c Is GetType(System.Windows.Forms.ComboBox) Then
                    c.Text = "NA"
                    c.Enabled = True
                    c.ForeColor = Color.Blue
                    c.BackColor = Color.Yellow
                End If
            Next
        Next



End if
 
Sorry

Dim t As TabControl

Should be

Dim t As TabControl = Ctype(Me.TabCtrl1.TabPages(0).Controls(i), System.Windows.Forms.TabControl)
 
Thanks for that, it helped a lot.
My working code now is like this:

Dim t As TabControl = CType(Me.TabControl1.TabPages(0).Controls(i), System.Windows.Forms.TabControl)
Dim p As TabPage
Dim tb As TextBox
Dim o As Integer
For Each p In t.TabPages
For Each c In p.Controls
If c.GetType Is GetType(System.Windows.Forms.TextBox) Then
CType(c, System.Windows.Forms.TextBox).ReadOnly = False
CType(c, System.Windows.Forms.TextBox).Text = ""
CType(c, System.Windows.Forms.TextBox).Enabled = True
CType(c, System.Windows.Forms.TextBox).ForeColor = m_Colour_PrefEditFG
CType(c, System.Windows.Forms.TextBox).BackColor = m_Colour_PrefEditBG
ElseIf c.GetType Is GetType(System.Windows.Forms.ComboBox) Then
CType(c, System.Windows.Forms.ComboBox).ReadOnly = False
CType(c, System.Windows.Forms.ComboBox).Enabled = True
CType(c, System.Windows.Forms.ComboBox).ForeColor = m_Colour_PrefEditFG
CType(c, System.Windows.Forms.ComboBox).BackColor = m_Colour_PrefEditBG
ElseIf c.GetType Is GetType(System.Windows.Forms.ListBox) Then
etc.......................
etc.......................
etc.......................
Endif
Next
Next


Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top