2015-05-27 121 views
0

我有一个winform,带有SplitContainer控件。在控制范围内,我有很多文本框和带有复选框的组框。我想清除非只读文本框,并取消选中任何点击按钮上的复选框。清除分隔符容器内的文本框控件

我试图复制该代码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 

要调用子:

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

我敢肯定,这是一个很小的细节我错过,任何人都可以找到适合我?

回答

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 

试试这个为你有的每个groupbox,改变星星的东西来满足你的需求。

 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 
+0

就像一个魅力。谢谢! – alybaba726

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