清除 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:21:52  来源:igfitidea点击:

Clearing check boxes in VB.NET

vb.netvisual-studio-2010vb.net-2010

提问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.CheckStateand element.Checkedand both times I get "Checked (or CheckState) is not a member of System.Windows.Forms.Control"

我试过使用element.CheckStateandelement.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 elementas? If its just a Controlthen this is a base type for CheckBoxthat 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