VB.NET验证复选框

时间:2016-03-09 07:23:42

标签: vb.net checkbox

如果选中多个复选框,我如何检查单个复选框,即我有51个复选框,如果检查了50则将检查单个剩余复选框。我该怎么做?

这是我的代码:

For Each chk In Me.Controls
    If TypeOf chk Is CheckBox Then
        If CType(chk, CheckBox).Checked = True Then
                chkAll.Checked = True
        End If
    End If
Next

如果这是正确的那么我的问题是我不知道在哪里放这个。因为它不应该在Button

3 个答案:

答案 0 :(得分:1)

创建检查所有CheckBoxes

的函数
Public Function IsAllCheckBoxesChecked() As Boolean
    Return Me.Controls.
              Cast(Of CheckBox).
              All(Function(chk) chk.Equals(Me.chkAll) = False AndAlso chk.Checked = True)
End Function

或LINQ方法

Private Sub CheckBoxes_CheckedChange(sender As Object, e As EventArgs)
    Dim chk As CheckBox = DirectCast(sender, CheckBox)
    If chk.Checked = True Then
        ' Check if all CheckBoxes are checked
        Me.chkAll.Checked = IsAllCheckBoxesChecked()
    Else
        ' No need to loop other chekboxes, anyway it is false
        Me.chkAll.Checked = False
    End If
End Sub

然后为所有复选框创建CheckedChange事件处理程序并在那里使用那些函数

cordova plugin add https://github.com/EddyVerbruggen/Insomnia-PhoneGap-Plugin.git

答案 1 :(得分:1)

首先,为所有复选框准备CheckedChanged事件处理程序:

Private Sub CheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged, CheckBox3.CheckedChanged, CheckBox4.CheckedChanged 'etc..
    .
    .
    .
End Sub

然后在您的事件处理程序中,使用LINQ WhereSelect来最好地解决您的问题:

Dim allCheckBoxes = Me.Controls.Cast(Of Control)().Where(Function(x) TypeOf x Is CheckBox).Select(Function(y) CType(y, CheckBox))
Dim thisCb = CType(sender, CheckBox)
If allCheckBoxes.Where(Function(x) x.Checked).ToList().Count >= 50 And thisCb.Checked Then 'if atleast 50 checkboxes are checked and the current one is checked
    Dim nonCheckedCheckBoxes = allCheckBoxes.Where(Function(t) Not t.Checked).ToList()
    nonCheckedCheckBoxes.ForEach(Function(t) t.Checked = True)
End If

以上LINQ的作用基本上是懒惰地获取所有CheckBoxes

Dim allCheckBoxes = Me.Controls.Cast(Of Control)().Where(Function(x) TypeOf x Is CheckBox).Select(Function(y) CType(y, CheckBox))

然后,检查已检查CheckBoxes的数量是否等于50并检查发件人(CheckBox)。如果未检查发件人,则我们省略它,因为用户可能无法取消选中(更正记入Fabio):

If allCheckBoxes.Where(Function(x) x.Checked).ToList().Count = 50 And thisCb.Checked Then 'if 50 checkboxes are checked

如果是,则检查剩余的未检查CheckBoxes

Dim nonCheckedCheckBoxes = allCheckBoxes.Where(Function(t) Not t.Checked).ToList()
nonCheckedCheckBoxes.ForEach(Function(t) t.Checked = True)

答案 2 :(得分:0)

将此设置为所有CheckBoxes的处理程序:

Private Sub CheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged, CheckBox3.CheckedChanged ' And so on...
    Dim allChecked As Boolean = True

    For Each chk In Me.Controls
        If TypeOf chk Is CheckBox Then
            If chk.Equals(chkAll) Then
                Continue For
            End If

            If CType(chk, CheckBox).Checked = False Then
                allChecked = False

                Exit For
            End If
        End If
    Next

    chkAll.Checked = allChecked
End Sub