【问题标题】:How to check if a checkbox is checked when iterating through form controls遍历表单控件时如何检查复选框是否被选中
【发布时间】:2013-05-15 16:27:54
【问题描述】:

我正在尝试为表单上的每个复选框设置一个注册表项,但在以下代码块中,我收到了错误 'Checked' is not a member of 'System.Windows.Forms.Control'

有人可以帮我找出我收到此错误的原因吗?

' Create the data for the 'Servers' subkey
Dim SingleControl As Control    ' Dummy to hold a form control
For Each SingleControl In Me.Controls
    If TypeOf SingleControl Is CheckBox Then
        Servers.SetValue(SingleControl.Name, SingleControl.Checked) ' Error happening here
    End If
Next SingleControl

【问题讨论】:

    标签: vb.net forms checkbox


    【解决方案1】:

    在使用 Checked 属性之前,您应该将控件转换为 CheckBox。
    您直接使用 Control 变量,并且这种类型(Control)没有 Checked 属性

    Dim SingleControl As Control    ' Dummy to hold a form control
    For Each SingleControl In Me.Controls
        Dim chk as CheckBox = TryCast(SingleControl, CheckBox)
        If chk IsNot Nothing Then
            Servers.SetValue(chk.Name, chk.Checked) 
        End If
    Next 
    

    更好的方法是使用Enumerable.OfType

    Dim chk As CheckBox
    For Each chk In Me.Controls.OfType(Of CheckBox)()
        Servers.SetValue(chk.Name, chk.Checked) 
    Next 
    

    这消除了将泛型控件转换为正确类型并测试转换是否成功的需要

    【讨论】:

    • 谢谢。我发现以这种方式循环不会遍历组中的控件,因此必须稍微更改为For Each chk In Me.grpServers.Controls.OfType(Of CheckBox)(),但您的两个示例都有效。
    • 是的,该示例仅适用于表单控件容器。如果您的复选框在 groupBox 上,那么您需要使用 Groupbox 的控件容器
    【解决方案2】:

    试试这个代码,

    Dim SingleControl As Control
    For Each SingleControl In Me.Controls
        If TypeOf SingleControl Is CheckBox Then
            'control does not have property called checked, so we have to cast it into a check box.
            Servers.SetValue(CType(SingleControl, CheckBox).Name, CType(SingleControl, CheckBox).Checked)         End If
    Next SingleControl
    

    【讨论】:

      【解决方案3】:

      CheckedCheckBox 类的属性,而不是其 Control 父类的属性。

      您必须将Control 向下转换为Checkbox 才能访问属性Checked,或者您必须将复选框存储为CheckBox 集合而不是Control 集合。

      【讨论】:

        【解决方案4】:

        试试这个:

        For Each SingleControl As Control In Me.Controls
            If TypeOf SingleControl Is CheckBox Then
                Dim auxChk As CheckBox = CType(SingleControl, CheckBox)
                Servers.SetValue(auxChk.Name, auxChk.Checked)
            End If
        Next SingleControl
        

        【讨论】:

          【解决方案5】:

          使用我的扩展方法获取表单上的所有控件,包括表单中其他容器内的控件,即面板、组框等。

          <Extension()> _
          Public Function ChildControls(Of T As Control)(ByVal parent As Control) As List(Of T)
              Dim result As New ArrayList()
              For Each ctrl As Control In parent.Controls
                  If TypeOf ctrl Is T Then result.Add(ctrl)
                  result.AddRange(ChildControls(Of T)(ctrl))
              Next
              Return result.ToArray().Select(Of T)(Function(arg1) CType(arg1, T)).ToList()
          End Function
          

          用法:

          Me.ChildControls(Of CheckBox). _
              ForEach( _
                  Sub(chk As CheckBox)
                      Servers.SetValue(chk.Name, chk.Checked)
                  End Sub)
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-03-02
            • 2018-10-14
            • 2016-02-16
            • 1970-01-01
            • 2019-01-12
            相关资源
            最近更新 更多