清除 VB.NET 中的复选框

发布于 2024-10-22 07:26:05 字数 876 浏览 1 评论 0原文

我正在为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

许久 2024-10-29 07:26:05

您将 element 声明为什么类型?如果它只是一个Control,那么这是没有checked 属性的CheckBox 的基本类型。也许尝试:

If TypeOf element Is CheckBox Then
    DirectCast(element,CheckBox).checked = False
End If

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:

If TypeOf element Is CheckBox Then
    DirectCast(element,CheckBox).checked = False
End If
往日 2024-10-29 07:26:05

怎么样:

   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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文