VB取消选中表格

时间:2016-01-11 11:49:21

标签: vb.net

这是我的form1,它包含许多复选框和一个按钮,用于显示form2:

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Form2.show()
    End Sub
End Class

这是我的form2,它还包含许多复选框和一个按钮,用于取消选中form1和form2上的所有复选框:

Public Class Form2
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'code for clearing all the checkboxes, I don't know how to do it
    End Sub
End Class

我的问题是:如何在Form2.Button1中创建一个代码来取消选中Form1,Form2甚至其他形式的所有复选框?

<小时/> 我尝试了这个代码,只取消选中表单中的复选框:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    For Each cc As Control In Me.Controls
        If TypeOf cc Is CheckBox Then
            DirectCast(cc, CheckBox).Checked = False
        End If
    Next
End Sub

也许我可以通过编辑这段代码来做到这一点..我不知道 提前谢谢大家

3 个答案:

答案 0 :(得分:1)

你需要在一个中心位置保留一个打开表单的列表,当创建任何表单时,它应该将自己添加到列表中,并在处理时自行删除(你可以从执行此操作的公共基表单继承所有表单) )

然后,您可以将所有表单提供给一个类似的方法,迭代它们并完成工作。

此外,请注意,您的代码不会取消选中表单上的所有复选框,只需取消选中表单上的直接复选框。如果您可能在面板,组框等上有chackbox,则需要改进代码以递归检查内部控件。

答案 1 :(得分:1)

可能@Alireza说你需要递归检查内部控件,希望以下是你想要的,
谢谢,

对于每个表单,您只需要插入

Dim x As New Class1     
x.TempClass(Me)

例如代码

Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim x As New Class1
            x.TempClass(Me)
        End Sub
End Class

然后你应该为所有CheckBoxs动作创建一个新类,如下面的

Public Class Class1
    Public Function TempClass(ByRef form As Control)

        Dim allTxt As New List(Of Control)
        For Each txt As CheckBox In FindControlRecursive(allTxt, form, GetType(CheckBox))
            txt.Enabled = False
            txt.Checked = False
        Next
        Return form
    End Function



    Public Shared Function FindControlRecursive(ByVal list As List(Of Control), ByVal parent As Control, ByVal ctrlType As System.Type) As List(Of Control)
        If parent Is Nothing Then Return list
        If parent.GetType Is ctrlType Then
            list.Add(parent)
        End If
        For Each child As Control In parent.Controls
            FindControlRecursive(list, child, ctrlType)
        Next
        Return list
    End Function
End Class

答案 2 :(得分:1)

最后我得到了一个解决方案:

#!/bin/sh

dialog --menu \
"Please select a partition from the following list to use for your \
root (/) Linux partition." 13 70 3 \
"/dev/hda2" "Linux native 30724312K" "/dev/hda4" "Linux native 506047K"


DISKS='"disk1" "50 Gb" "disk2" "100 Gb"'

dialog --menu \
"Please select a partition from the following list to use for your \
root (/) Linux partition." 13 70 3 \
$DISKS