I have a form that has a tabcontrol (TabControl1) and within that tab control is another one (TabControl2). I have textboxes on the main form as well as in each of the tabcontrols.

In my savebutton event I have the following code:

If CheckIfDirtyAfterLock = "Just Unlocked" Then
    'Do Nothing
End If

This calls on the following:

Private Sub CheckIfDirty(ByVal ctrls As Control.ControlCollection)
        For Each ctrl As Control In ctrls
            If TypeOf ctrl Is TextBox Then

                If DirectCast(ctrl, TextBox).Modified Then
                    Dim strOut As String = ""
                    strOut = String.Format("{0} was modified on " & Date.Today.ToShortDateString, DirectCast(ctrl, TextBox).Name)

                    If Addendum.Text = "" Then
                        Addendum.Text = strOut
                        Addendum.Text = Addendum.Text & " | " & strOut
                        Addendum.Text = Addendum.Text.Trim
                    End If

                End If

                ' check child controls if any
            End If

    Catch ex As Exception

    End Try
End Sub

What happens is that it records those textboxes that have changed and writes it to addendum.text. The problem is that it is only capturing those textboxes on my main form and none of the textboxes that get changed within TabControl1 or TabControl2. Anyone have any suggestions as to why this is happening?

Unhnd_Exception commented: Question but no status -2

Recommended Answers

All 5 Replies

You will have to right a for/each loop for each sub level.

Each level is considered it's own collection.


For Each i As Control In Me.Controls
        For Each j As Control In i.Controls
            For Each k As Control In j.Controls
                If TypeOf k Is TextBox Then

                End If
            If TypeOf j Is TextBox Then

            End If
        If TypeOf i Is TextBox Then

        End If

That sounds like it makes perfect sense. I will give it a shot and let you know. Thank you :)

Ok. Just remeber that for every control that contains other controls, you will need another For loop.


Dim controls As TextBox() = Me.Controls.OfType(Of TextBox)().OrderBy(Function(o) o.Name).ToArray()
'then you can loop in foreach (like bellow)

For Each control As var In Me.Controls.OfType(Of TextBox)()
    Dim name As String = control.Name
commented: Good alternative! +5
Member Avatar

I'm not sure if the above .OfType checks all children.

If it does not then you can use this block of code to get every textbox including all child controls.

        Dim StackOfControls As New Stack(Of Control)
        Dim AllTextBoxes As New List(Of TextBox)
        Dim ControlChecking As Control
        Do While StackOfControls.Count > 0
              ControlChecking = StackOfControls.Pop
              For Each ChildControl As Control In ControlChecking.Controls
                 If TypeOf ChildControl Is TextBox Then
                 End If
                 If ChildControl.Controls.Count > 0 Then
                 End If

         'All TextBoxes inside AllAllTextBoxes'
         'Iterate through the list and do what you want'
         For Each FoundTextBox In AllTextBoxes
             'Do what you want to any textbox on the form'
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.