我有一个SplitContainer
控件的winform。在控件中,我有许多文本框和带复选框的组合框。我想清除非ReadOnly文本框,并在单击按钮时取消选中任何选中的复选框。
我尝试复制此代码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
调用sub:
Private Sub Clear_Click(sender As Object, e As EventArgs) Handles Clear.Click
ClearRecord(Me.CntrLOMC.Panel1, True)
End Sub
但是得到错误:
Value of type 'System.Windows.Forms.GroupBox' cannot be converted to 'System.Windows.Forms.SplitterPanel'.
我也没有回答这些解决方案:
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
我确定这是一个我遗漏的小细节,有人能为我找到它吗?
答案 0 :(得分:3)
尝试使用splitcontainer中的每个面板,更改星星中的内容以满足您的需求。
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
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
希望这有帮助!
** 编辑阅读 **
这是一种更有效的方法,但它只会更改splitcontainer中最多两个子节点的属性。
首先创建这个子:
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
然后按照你喜欢的方式使用它。 (调用子按钮或单击按钮或任何按钮。)
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
答案 1 :(得分:0)
以下是使用原始方法的方法:
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