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?