代码之家  ›  专栏  ›  技术社区  ›  arc95

标记多个复选框

  •  0
  • arc95  · 技术社区  · 7 年前

    我有两排 checkboxes 已命名。

    像这样:

    上部(ck18、ck17、ck16、ck15、ck14、ck13、ck12、ck11、ck21、ck22、ck23、ck24、ck25、ck26、ck27、ck28)

    下部(ck38、ck37、ck36、ck35、ck34、ck33、ck32、ck31、ck41、ck42、ck43、ck44、ck45、ck46、ck47、ck48)

    按以下顺序排序:

    按下shift键时,有一种简单的方法可以选中一系列复选框?(就像word上的文本选择)=只需标记第一个,然后是最后一个,同时按下shift键可自动标记中间复选框。

    2 回复  |  直到 7 年前
        1
  •  1
  •   Pedro Gaspar    7 年前

    考虑到您正在谈论Windows窗体,您可以使用以下代码:

    Public Class Form1
    
       Private _upperChkList As List(Of CheckBox)
       Private _lowerChkList As List(Of CheckBox)
       Private _firstChkClickedIndex As Integer = -1
       Private _firstChkClickedState As CheckState = CheckState.Indeterminate
    
       Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    
          ' Stores all the upper CheckBoxes in their specific list.
          _upperChkList = New List(Of CheckBox) From
             {ck18, ck17, ck16, ck15, ck14, ck13, ck12, ck11,
             ck21, ck22, ck23, ck24, ck25, ck26, ck27, ck2}
          ' Stores all the lower CheckBoxes in their specific list.
          _lowerChkList = New List(Of CheckBox) From
             {ck38, ck37, ck36, ck35, ck34, ck33, ck32, ck31,
             ck41, ck42, ck43, ck44, ck45, ck46, ck47, ck48}
    
          ' Defines the Click event handler for all the CheckBoxes.
          For Each chk In _upperChkList
             AddHandler chk.Click, AddressOf UpperCheckBoxes_Click
          Next
          For Each chk In _lowerChkList
             AddHandler chk.Click, AddressOf LowerCheckBoxes_Click
          Next
    
       End Sub
    
       Private Sub UpperCheckBoxes_Click(sender As Object, e As EventArgs)
    
          ' Redirects to the procedure that manages the CheckBoxes selection, passing
          ' the desired list to be handled, the upper CheckBoxes list, in this case.
          OnCheckBoxesClick(sender, _upperChkList)
    
       End Sub
    
       Private Sub LowerCheckBoxes_Click(sender As Object, e As EventArgs)
    
          ' Redirects to the procedure that manages the CheckBoxes selection, passing
          ' the desired list to be handled, the lower CheckBoxes list, in this case.
          OnCheckBoxesClick(sender, _lowerChkList)
    
       End Sub
    
       Private Sub OnCheckBoxesClick(sender As Object, chkList As List(Of CheckBox))
    
          ' Converts the sender from Object to Checkbox.
          Dim chk = CType(sender, CheckBox)
    
          ' If Shift key is not pressed, stores the index of the 
          ' first CheckBox pressed, And its state.
          If Control.ModifierKeys <> Keys.Shift Then
             ' Searches the CheckBox being clicked in the list and returns its index.
             ' Since Shift key is not pressed, that will be the first CheckBox.
             _firstChkClickedIndex = chkList.FindIndex(Function(c) c.Equals(chk))
             _firstChkClickedState = chk.CheckState
             Return
          End If
    
          ' If it got here, Shift key is pressed, so, it must have
          ' a first CheckBox stored. If not, don't go on.
          If _firstChkClickedIndex < 0 Then
             Return
          End If
          ' If the state of the actual CheckBox is different than the 
          ' state of the first one, don't go on.
          If chk.CheckState <> _firstChkClickedState Then
             Return
          End If
    
          ' Searches the CheckBox being clicked in the list and returns its index.
          ' Since Shift key is pressed, that will be the last CheckBox.
          Dim lastChkClickedIndex = chkList.FindIndex(Function(c) c.Equals(chk))
    
          ' Checks if we are going from a lower to a higher index, or the contrary.
          Dim stepDirection As Integer = If(lastChkClickedIndex >= _firstChkClickedIndex, 1, -1)
          ' Iterates the list from the first to the last CheckBoxes clicked 
          ' and changes the state of all the CheckBoxes in between to match 
          ' the state of the first CheckBox clicked.
          For i = _firstChkClickedIndex To lastChkClickedIndex Step stepDirection
             chkList(i).CheckState = _firstChkClickedState
          Next
    
          ' Resets the first CheckBox variables.
          _firstChkClickedIndex = -1
          _firstChkClickedState = CheckState.Indeterminate
    
       End Sub
    
    End Class
    

    我猜这些复选框在表单中的排列顺序与您在此处放置的顺序相同,因此,这就是我用来填充列表的顺序。如果这不是正确的视觉顺序,则必须修复它们添加到列表中的顺序。

        2
  •  0
  •   Mary    7 年前

    如果有一个按钮,上面写着选中所有框,然后单击按钮调用此Sub。

    Private Sub CheckThemAll()
            For Each ctr As Control In Controls
                If TypeOf ctr Is CheckBox Then
                    CType(ctr, CheckBox).Checked = True
                End If
            Next
    End Sub