Ok i am having a blond moment(no offense haha).

I got on my form textboxes, checboxes but also a groupbox which holds texboxes.

The idea is to be able to clear and reset all of these by clicking a reset button.

At the moment i got this

Private Sub ClearFields()
        Dim ctrl As Control
        For Each ctrl In Me.Controls
            If ctrl.GetType Is GetType(TextBox) Then
                ctrl.Text = ""
            ElseIf ctrl.GetType Is GetType(CheckBox) Then
                Dim chkbx As CheckBox = ctrl
                chkbx.Checked = False
            End If
        Next

    End Sub

But ofc Me.controls isn't gonna get involved with the stuff in the groupbox.

Can someone help me point out what i need to use again so it will will clear the textboxes in the groupbox also?

Thanks

Member Avatar for Unhnd_Exception

This will reset all textboxes and checkboxes inside all groupboxes on the form

For Each Control As Control In Me.Controls
     If TypeOf Control Is GroupBox Then
          
          For Each ChildControl As Control In Control.Controls
              Select Case ChildControl.GetType.ToString
                  
                  Case GetType(TextBox).ToString
                      ChildControl.Text = String.Empty
                  
                   Case GetType(CheckBox).ToString
                      CType(ChildControl, CheckBox).Checked = False
              
              End Select
          Next
       End If
  Next

Ah should have known that.

Thanks a lot for helping me out.
Works perfect.

Member Avatar for Unhnd_Exception

You may want to do it like this.

This is how i would do it. This way you don't have to change your code if you move the groupboxes into panels later on down the road.

Private Sub ButtonReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonReset.Click
        ResetGroupBox(GroupBox1)
        ResetGroupBox(GroupBox2)
    End Sub

    Private Sub ResetGroupBox(ByVal value As GroupBox)
        For Each Control As Control In value.Controls
            Select Case Control.GetType.ToString
                Case GetType(TextBox).ToString
                    Control.Text = String.Empty
                Case GetType(CheckBox).ToString
                    CType(Control, CheckBox).Checked = False
            End Select
        Next
    End Sub

Yes your right, thanks for the tip.

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.