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
    CheckIfDirty(Me.Controls)
Else
    'Do Nothing
End If

This calls on the following:

Private Sub CheckIfDirty(ByVal ctrls As Control.ControlCollection)
    Try
        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
                    Else
                        Addendum.Text = Addendum.Text & " | " & strOut
                        Addendum.Text = Addendum.Text.Trim
                    End If

                End If

                ' check child controls if any
                CheckIfDirty(ctrl.Controls)
            End If
        Next

    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?

Comments
Question but no status

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

Each level is considered it's own collection.

Example:

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
            Next
            If TypeOf j Is TextBox Then

            End If
        Next
        If TypeOf i Is TextBox Then

        End If
    Next

Edited 4 Years Ago by Begginnerdev: edit

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

or;

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

'2.
For Each control As var In Me.Controls.OfType(Of TextBox)()
    Dim name As String = control.Name
Next
Comments
Good alternative!

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
        StackOfControls.Push(Me)
        Do While StackOfControls.Count > 0
              ControlChecking = StackOfControls.Pop
              For Each ChildControl As Control In ControlChecking.Controls
                 If TypeOf ChildControl Is TextBox Then
                      AllTextBoxes.Add(ChildControl)
                 End If
                 If ChildControl.Controls.Count > 0 Then
                      StackOfControls.Push(ChildControl)
                 End If
              Next
         Loop

         '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'
         Next

Edited 4 Years Ago by Unhnd_Exception

This article has been dead for over six months. Start a new discussion instead.