alybaba726
alybaba726

Reputation: 400

Clearing textbox controls within splitter container

I have a winform with SplitContainer control. Within the control, I have many textboxes and a groupbox with checkboxes. I want to clear the non-ReadOnly textboxes and uncheck any checked checkboxes on a button click.

I tried to replicate this code VB.NET - Iterating through controls in a container object

   Public Sub ClearRecord(ByRef container As SplitterPanel, Optional recurse As Boolean = True)
    'For Each tbx As TextBox In Me.Controls.OfType(Of TextBox)()
    '    If Not tbx.ReadOnly Then
    '        tbx.Text = String.Empty
    '        tbx.BackColor = SystemColors.Window
    '    End If
    'Next

    'For Each chkbx As CheckBox In Me.Controls.OfType(Of CheckBox)()
    '    chkbx.Checked = False
    'Next
    Dim cntrl As Control
    For Each cntrl In container.Controls
        If (cntrl.GetType() Is GetType(TextBox)) Then
            Dim txt As TextBox = CType(cntrl, TextBox)
            If txt.ReadOnly = False Then
                txt.Text = String.Empty
            End If
        End If

        If (cntrl.GetType() Is GetType(CheckBox)) Then
            Dim chk As CheckBox = CType(cntrl, CheckBox)
            chk.Checked = False
        End If
    Next

    If recurse = True Then
        If (cntrl.GetType() Is GetType(GroupBox)) Then
            Dim grpbx As GroupBox = CType(cntrl, GroupBox)
            ClearRecord(grpbx, recurse)
        End If
    End If


    Me.lblInvalid.Visible = False
    Me.lblAddrInv.Visible = False
    Me.lblZipInv.Visible = False
    Me.lblInvFZ.Visible = False
    Me.lblInvBFE.Visible = False
    Me.lblInvalidDepth.Visible = False

End Sub

To call the sub:

Private Sub Clear_Click(sender As Object, e As EventArgs) Handles Clear.Click
    ClearRecord(Me.CntrLOMC.Panel1, True)
End Sub

But get the error :

Value of type 'System.Windows.Forms.GroupBox' cannot be converted to 'System.Windows.Forms.SplitterPanel'.

I have also looked into these solutions with no answer:

Looping through Controls in VB.NET

VB.NET Loop through controls in a panel skips controls

Looping through Controls in VB.NET

Clearing many textbox controls in vb.net at once

I'm sure it's a small detail I'm missing, can anyone find it for me?

Upvotes: 0

Views: 627

Answers (2)

Jordan
Jordan

Reputation: 323

Try this for each panel you have in your splitcontainer, changing the things in stars to suit your needs.

    Try
        For Each item In **SplitContainer1.Panel1**.Controls
            If TypeOf item Is TextBox Then
                item.Text = ""
            ElseIf TypeOf item Is CheckBox Then
                item.checked = False
            End If
        Next
    Catch ex As Exception
        MsgBox(ex.ToString(), MsgBoxStyle.Exclamation, "Error")
    End Try

Try this for each groupbox you have, changing the things in stars to suit your needs.

        Try
        For Each item In **GroupBox1**.Controls
            If TypeOf item Is TextBox Then
                item.Text = ""
            ElseIf TypeOf item Is CheckBox Then
                item.checked = False
            End If
        Next
        Catch ex As Exception
            MsgBox(ex.ToString(), MsgBoxStyle.Exclamation, "Error")
        End Try

Hope this helps!

** EDIT READ ME **

This is a much more efficient method however it will only change the properties of up to two children in the splitcontainer.

First create this sub:

Public Sub resetControls(control)
    If TypeOf control Is TextBox Then
        control.Text = ""
    ElseIf TypeOf control Is CheckBox Then
        control.checked = False
    End If
End Sub

Then use this however you like. (calling a sub or on a button click or what ever.)

    Try
        For Each control In SplitContainer1.Controls
            resetControls(control)
            For Each child In control.controls
                resetControls(child)
                For Each child1 In child.controls
                    resetControls(child1)
                Next
            Next
        Next
    Catch ex As Exception
        MsgBox(ex.ToString(), MsgBoxStyle.Exclamation, "Error")
    End Try

Upvotes: 3

Idle_Mind
Idle_Mind

Reputation: 39122

Here's how to do it using your original approach:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ClearRecord(CntrLOMC.Panel1, True)
    Me.lblInvalid.Visible = False
    Me.lblAddrInv.Visible = False
    Me.lblZipInv.Visible = False
    Me.lblInvFZ.Visible = False
    Me.lblInvBFE.Visible = False
    Me.lblInvalidDepth.Visible = False
End Sub

Public Sub ClearRecord(ByVal container As Control, Optional recurse As Boolean = True)
    For Each cntrl As Control In container.Controls
        If TypeOf cntrl Is TextBox Then
            Dim txt As TextBox = CType(cntrl, TextBox)
            If txt.ReadOnly = False Then
                txt.Text = String.Empty
            End If
        ElseIf TypeOf cntrl Is CheckBox Then
            Dim chk As CheckBox = CType(cntrl, CheckBox)
            chk.Checked = False
        ElseIf cntrl.HasChildren AndAlso recurse Then
            ClearRecord(cntrl, recurse)
        End If
    Next
End Sub

Upvotes: 0

Related Questions