Windows 窗体 CheckedListBox 控件扩展了 ListBox 控件。它几乎能完成列表框可以完成的所有任务,并且还可以在列表中的项旁边显示复选标记。两种控件间的其他差异在于,复选列表框只支持 DrawMode.Normal,并且复选列表框只能有一项选中或未选中任何项。注意选定的项在窗体上突出显示,与已选中的项不同。
以下代码演示了CheckedListBox 的全选与反选
折叠ASP/Visual Basic Code复制内容到剪贴板
- Private Sub chkAll_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkAll.CheckedChanged '全选控件代码
- If chkAll.Checked = True Then
- For u As Integer = 0 To CheckedListBox1.Items.Count - 1
- CheckedListBox1.SetItemChecked(u, True)
- Next
- Else
- For u As Integer = 0 To CheckedListBox1.Items.Count - 1
- CheckedListBox1.SetItemChecked(u, False)
- Next
- End If
- End Sub
- Private Sub chkOpp_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkOpp.CheckedChanged '反选控件代码
- For u As Integer = 0 To CheckedListBox1.Items.Count - 1
- CheckedListBox1.SetItemChecked(u, Not (CheckedListBox1.GetItemChecked(u))) '反选
- Next
- End Sub