清除 VB.NET 中的复选框
我正在为 Uni 做作业,在我的 VB.NET 表单中我有一些复选框,我试图循环并清除它们(我有一个按钮可以清除表单)
我的问题是似乎没有当没有明确告诉 VB 我要使用哪个复选框时,我可以使用属性来设置复选框的状态。例如,我可以去
WineCheckBox.Checked = False
这将选中该框,但我想稍微干燥一下代码,而不必为我拥有的每个复选框重复此操作,这就是我想要做的:
If TypeOf element Is CheckBox Then
element.Checked = False
End If
我尝试使用 < code>element.CheckState 和 element.Checked
两次我都得到“Checked(或 CheckState)不是 System.Windows.Forms.Control 的成员”
我已经查看了所有我可以为此找到的属性,但它们似乎对我来说都没有用......
我错过了什么吗?或者这是不可能做到的
谢谢
编辑:
这是整个代码块:
'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
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)
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
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"
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将
element
声明为什么类型?如果它只是一个Control
,那么这是没有checked 属性的CheckBox
的基本类型。也许尝试:What type have you declared
element
as? If its just aControl
then this is a base type forCheckBox
that doesn't have the checked property. Maybe try:怎么样:
How about: