vb.net 遍历表单控件时如何检查复选框是否被选中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15876229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if a checkbox is checked when iterating through form controls
提问by David Gard
I'm trying to set a registry key for every checkbox on a form, but in the following block of code, I'm receiving the error 'Checked' is not a member of 'System.Windows.Forms.Control'
我正在尝试为表单上的每个复选框设置一个注册表项,但在以下代码块中,我收到错误 'Checked' is not a member of 'System.Windows.Forms.Control'
Can somebody please help me find out why I'm getting this error?
有人可以帮我找出为什么我收到这个错误吗?
' 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
回答by Steve
You should convert your control to a CheckBox before using the Checked property.
You use directly the Control variable and this type (Control) doesn't have a Checked property
在使用 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
A better approach could be using Enumerable.OfType
更好的方法可能是使用 Enumerable.OfType
Dim chk As CheckBox
For Each chk In Me.Controls.OfType(Of CheckBox)()
Servers.SetValue(chk.Name, chk.Checked)
Next
this removes the need to convert the generic control to a correct type and test if the conversion was successfully
这消除了将通用控件转换为正确类型并测试转换是否成功的需要
回答by Sathish
Try this code,
试试这个代码,
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
回答by zakinster
Checkedis a property of the CheckBoxclass, not its Controlparent.
Checked是CheckBox类的属性,而不是Control其父类。
You either have to downcast the Controlinto a Checkboxin order to access the property Checkedor you have to store your checkboxes as a CheckBoxcollection not a Controlcollection.
您要么必须向下转换Control成 aCheckbox才能访问该属性,Checked要么必须将复选框存储为CheckBox集合而不是Control集合。
回答by SysDragon
Try this:
尝试这个:
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
回答by djv
Use my extension method to get all controls on the form, including controls inside other containers in the form i.e. panels, groupboxes, etc.
使用我的扩展方法获取表单上的所有控件,包括表单中其他容器内的控件,即面板、分组框等。
<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
Usage:
用法:
Me.ChildControls(Of CheckBox). _
ForEach( _
Sub(chk As CheckBox)
Servers.SetValue(chk.Name, chk.Checked)
End Sub)

