Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net ';对于';用于分析面板中复选框的循环_Vb.net_Visual Studio_Visual Studio 2012_For Loop_Checkbox - Fatal编程技术网

Vb.net ';对于';用于分析面板中复选框的循环

Vb.net ';对于';用于分析面板中复选框的循环,vb.net,visual-studio,visual-studio-2012,for-loop,checkbox,Vb.net,Visual Studio,Visual Studio 2012,For Loop,Checkbox,我必须分析复选框是否被选中。选项卡页中有10个CBs,它们按顺序命名(cb1、cb2、cb3..等等) 我已经尝试了上述方法,但它给了我一个未处理的异常错误 An unhandled exception of type 'System.InvalidCastException' occurred in WindowsApplication2.exe Additional information: Unable to cast object of type 'System.Windows.For

我必须分析复选框是否被选中。选项卡页中有10个CBs,它们按顺序命名(cb1、cb2、cb3..等等)

我已经尝试了上述方法,但它给了我一个未处理的异常错误

An unhandled exception of type 'System.InvalidCastException' occurred in WindowsApplication2.exe 
Additional information: Unable to cast object of type 'System.Windows.Forms.Label' to type 'System.Windows.Forms.CheckBox'.

由于页面上可能有其他控件,因此您需要查看每个控件是否为支票:

For Each c As Control In TabPage4.Controls
    if Typeof c is CheckBox then
        if Ctype(c, Checkbox).Checked Then
            hello +=1
        End If
    End If
Next
根据您的VS版本,这可能会起作用(需要LINQ):

编辑

我猜您的
Ctype
部分出了点问题,因为您的数组所做的只是将Ctl转换为检查(Ctype所做的),但转换的方式更为昂贵。如果您不喜欢Ctype(并且不能使用第二种方式):


没有数组,没有额外的对象引用。

我对@puropoix代码做了一些更改,并使其正常工作。代码如下:

Dim n As Integer = 1
For Each c As Control In TabPage4.Controls
        If TypeOf c Is CheckBox Then
            CBs(n) = c
            If CBs(n).Checked Then
                hello = hello + 1
            End If
        End If
        n = n + 1
    Next

Cbs(n)是我在模块上创建的一组复选框。它将“c”复选框声明为Cbs(n),并对此进行分析。然后,它将1添加到变量n中,并重新启动该过程,直到选项卡页中没有更多复选框为止。

在这种情况下,可能不需要这样做,但有时需要“按顺序”获取它们。下面是一个例子:

    Dim cb As CheckBox
    Dim hello As Integer
    Dim matches() As Control
    For i As Integer = 1 To 10
        matches = Me.Controls.Find("cb" & i, True)
        If matches.Length > 0 AndAlso TypeOf matches(0) Is CheckBox Then
            cb = DirectCast(matches(0), CheckBox)
            If cb.Checked Then
                hello = hello + 1
            End If
        End If
    Next

您得到的确切错误是什么?@nhgrif这里是:WindowsApplication2.exe中发生了类型为“System.InvalidCastException”的未处理异常。其他信息:无法将类型为“System.Windows.Forms.Label”的对象强制转换为类型为“System.Windows.Forms.CheckBox”。该程序工作正常,但不会更改hello.What的值
hello
是一个字符串,整数。。。?事实上是否有任何复选框被选中?您可以在
上设置一个断点,如果…
并查看出了什么问题,比如检测复选框。使用更新您的帖子以包含
hello
,您使用的循环hello是一个整数。是的,复选框被选中。我对你的代码做了一些修改,它工作得非常完美。
Dim chk As CheckBox
For Each c As Control In TabPage4.Controls
    if Typeof c is CheckBox then
        chk  =  Ctype(c, Checkbox)
        if chk.Checked Then
            hello +=1
        End If
    End If
Next
Dim n As Integer = 1
For Each c As Control In TabPage4.Controls
        If TypeOf c Is CheckBox Then
            CBs(n) = c
            If CBs(n).Checked Then
                hello = hello + 1
            End If
        End If
        n = n + 1
    Next
    Dim cb As CheckBox
    Dim hello As Integer
    Dim matches() As Control
    For i As Integer = 1 To 10
        matches = Me.Controls.Find("cb" & i, True)
        If matches.Length > 0 AndAlso TypeOf matches(0) Is CheckBox Then
            cb = DirectCast(matches(0), CheckBox)
            If cb.Checked Then
                hello = hello + 1
            End If
        End If
    Next