【问题标题】:VB.NET Validating CheckboxesVB.NET 验证复选框
【发布时间】:2016-03-09 16:23:53
【问题描述】:

如果选中多个复选框,我如何检查单个复选框,即我有 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 中。

【问题讨论】:

  • 有LINQ解决方案可以吗?
  • 没关系,给懂LINQ的人用,因为我不会=)
  • 好的,发布了一个 LINQ 版本的答案。 :)

标签: vb.net checkbox


【解决方案1】:

创建检查所有复选框的函数

Public Function IsAllCheckBoxesChecked() As Boolean
    Dim isAllChecked As Boolean = True
    For Each chk As CheckBox In Me.Controls.Cast(Of CheckBox)
        If checkbox.Equals(Me.chkAll) = True Then Continue For
        If checkBox.Checked = false Then
            isAllChecked = false
            Exit For
        End If
    Next
    Return isAllChecked
End Function

或 LINQ 方法

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

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

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

【讨论】:

    【解决方案2】:

    首先,为所有复选框准备 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)
    

    【讨论】:

    • 使用这种方法,如果所有复选框(51)都被选中,那么用户将无法取消选中任何其他复选框。当用户取消选中某些复选框时,您的代码将计算出此时选中的集合包含 50 个复选框,并会自动将未选中的复选框重新选中
    【解决方案3】:

    将此设置为所有复选框的处理程序:

    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
    

    【讨论】:

    • 我应该把这个放在哪里?在chkAll_CheckedChanged?它不起作用,我无法取消选中 chkAll 复选框控件。
    • 我不知道这是可能的!谢谢!
    • 如果chkAll 控件在然后Me.Controls 集合中(我假设是),那么这个解决方案将不起作用,因为chkAll.Checked 总是错误的
    • 可以检查chk是否为chkAll,然后忽略。
    猜你喜欢
    • 2015-08-29
    • 1970-01-01
    • 2013-04-19
    • 2012-03-03
    • 2011-10-11
    • 2018-12-14
    • 2016-09-17
    相关资源
    最近更新 更多