我添加了一个包含一些名称和全选选项的复选框列表。当我选择(全选)选项时,我可以选中复选框列表中的所有复选框。
问题在于,每当我取消选中复选框列表中的任何选项时,我都无法取消选中(全选)选项。
以下是为(全选)功能附加的vb.net代码。
Private Sub ChkLB_dl_name_cb_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles ChkLB_dl_name_cb.ItemCheck
If e.Index = 0 Then
Dim newCheckedState As Integer = e.NewValue
For i As Integer = 1 to ChkLB_dl_name_cb.Items.Count - 1
Me.ChkLB_dl_name_cb.SetItemCheckState(i, newCheckedState)
Next
End If
End Sub
以下是Windows表单上复选框列表的图片,供您参考。
答案 0 :(得分:0)
一般情况下,我会使用Boolean
变量来避免为您以编程方式更改的每个项调用此处理程序。这也将解决您无法取消选中第一项的问题:
Private updatingListProgramatically As Boolean = False
Private Sub ChkLB_dl_name_cb_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles ChkLB_dl_name_cb.ItemCheck
If updatingListProgramatically Then Return
If e.Index = 0 Then
updatingListProgramatically = True
For i As Integer = 1 To ChkLB_dl_name_cb.Items.Count - 1
Me.ChkLB_dl_name_cb.SetItemCheckState(i, e.NewValue)
Next
Else
Dim checked As Boolean = e.NewValue = CheckState.Checked
If Not checked Then
updatingListProgramatically = True
Me.ChkLB_dl_name_cb.SetItemCheckState(0, CheckState.Unchecked)
End If
End If
updatingListProgramatically = False
End Sub
Else
块似乎就是你所要求的。
答案 1 :(得分:0)
尝试为第一个添加Else' If'条件并添加第二个'如果'声明是否"全选"复选框是否未选中。
cd /path/to/folder && ls -1t | tail -n +6 | sudo xargs rm -rf
答案 2 :(得分:0)
如下所示,您应该获得所需的功能:
Private Sub ChkLB_dl_name_cb_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles ChkLB_dl_name_cb.ItemCheck
If e.Index = 0 Then
Dim newCheckedState As Integer = e.NewValue
For i As Integer = 1 To ChkLB_dl_name_cb.Items.Count - 1
Me.ChkLB_dl_name_cb.SetItemCheckState(i, newCheckedState)
Next
Else
'Remove the event-handler to prevent the ItemCheck-method from being called again
RemoveHandler ChkLB_dl_name_cb.ItemCheck, AddressOf ChkLB_dl_name_cb_ItemCheck
'If the item is being checked and all items are checked
'(except "Select All"), then check "Select All"
If e.NewValue = CheckState.Checked AndAlso
ChkLB_dl_name_cb.CheckedItems.Count + 1 = ChkLB_dl_name_cb.Items.Count - 1 Then
'Check "Select All"
ChkLB_dl_name_cb.SetItemCheckState(0, CheckState.Checked)
Else
'Uncheck "Select All"
ChkLB_dl_name_cb.SetItemCheckState(0, CheckState.Unchecked)
End If
'Re-Add the handler
AddHandler ChkLB_dl_name_cb.ItemCheck, AddressOf ChkLB_dl_name_cb_ItemCheck
End If
End Sub