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?

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.

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

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.

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
commented: Good alternative! +5
Member Avatar for Unhnd_Exception

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
Be a part of the DaniWeb community

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