how to iterate through the controls in a TabControl?

jalo

Freshman
Joined
Aug 9, 2005
Messages
28
i am trying to clear all the textboxes contained on all tabpages of a tabcontrol, which is enclosed inside a groupbox. what i have is probably very ugly (plus it doesn't work), so please help.....
(the line in red wouldn't compile)
Code:
Dim ctrl As Control
Dim ctrl1 As Control
Dim tBox As TextBox
Dim tBox1 As TextBox
Dim tbPage As TabPage

For Each ctrl In GroupBox2.Controls
    If TypeOf ctrl Is TextBox Then
        tBox = DirectCast(ctrl, TextBox)
        tBox.Clear()
    ElseIf ctrl Is TabControl1 Then
        Dim i As Integer
        For i = 0 To (Me.TabControl1.TabPages.Count - 1)
            [COLOR=Red] For Each ctrl1 In Me.TabControl1.TabPages.Item(i)[/COLOR]
                 If TypeOf ctrl1 Is TextBox Then
                     tBox1 = DirectCast(ctrl1, TextBox)
                     tBox.Clear()
                 End If

             Next
       Next
     End If
Next
 
Me.TabControl1.TabPages.Item(i) is not a collection of Control objects. You may want to try something like this:

Code:
Dim index As Integer = 0
For Each tab As TabPage In TabControl1.TabPages
  For Each ctrl As Control In tab.Controls
    If (TypeOf (ctrl) Is TextBox) Then
      ctrl.Text = String.Empty
    End If
  Next
Next
 
Back
Top