清除 VB.NET 中的复选框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5346787/
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
Clearing check boxes in VB.NET
提问by Zen
I'm doing an assignment for Uni and in my VB.NET form I have some checkboxes, I'm trying to loop through and clear them (I have a button which will clear the form)
我正在为 Uni 做一个作业,在我的 VB.NET 表单中我有一些复选框,我正在尝试循环并清除它们(我有一个按钮可以清除表单)
My problem is that there seems to be no property I can use to set the state of a checkbox when not explicitly telling VB which checkbox I want to use. for example, I can go
我的问题是,当没有明确告诉 VB 我想使用哪个复选框时,似乎没有我可以用来设置复选框状态的属性。例如,我可以去
WineCheckBox.Checked = False
That will check the box, but I wand to DRY the code up a bit and not have to repeat this for each check box I have, this is what I was trying to do:
这将选中该框,但我想稍微干一下代码,而不必为我拥有的每个复选框重复此操作,这就是我想要做的:
If TypeOf element Is CheckBox Then
element.Checked = False
End If
I've tried using element.CheckState
and element.Checked
and both times I get "Checked (or CheckState) is not a member of System.Windows.Forms.Control"
我试过使用element.CheckState
andelement.Checked
并且两次我都得到“Checked (or CheckState) is not a member of System.Windows.Forms.Control”
I've looked through all the attributes that I can find for this and none of them seem of use to me...
我已经查看了我能找到的所有属性,但似乎没有一个对我有用......
Am I missing something? or is this just not possible to do
我错过了什么吗?或者这是不可能做到的
Thanks
谢谢
EDIT:
编辑:
this is the whole block of code:
这是整个代码块:
'clear the controls
For Each element As Control In Me.Controls
If TypeOf element Is TextBox Then
element.Text = ""
End If
If TypeOf element Is CheckBox Then
element.Checked = False
End If
Next
回答by Jon Egerton
What type have you declared element
as? If its just a Control
then this is a base type for CheckBox
that doesn't have the checked property. Maybe try:
你声明为什么类型element
?如果它只是一个,Control
那么这是一个CheckBox
没有检查属性的基本类型。也许尝试:
If TypeOf element Is CheckBox Then
DirectCast(element,CheckBox).checked = False
End If
回答by Matt Wilko
How about:
怎么样:
For Each element As Control In Me.Controls
If TypeOf element Is TextBox Then
element.Text = ""
End If
If TypeOf element Is CheckBox Then
Dim chk As CheckBox = CType(element, CheckBox)
chk.Checked = False
End If
Next