Für "Spielereien" mit der CheckListBox verwende ich ein Programm bestehend aus einer Form mit
1 CheckedListBox
7 Button
mit folgendem Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Text = "Demoprogramm für CheckListBox"
' Checken mit einen Click
CheckedListBox1.CheckOnClick = True
FillCheckedListBox(CheckedListBox1)
Me.MinimumSize = Size
End Sub
' --------------------------
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
FillCheckedListBox(CheckedListBox1)
End Sub
Sub FillCheckedListBox(ByVal ChLB As CheckedListBox)
CheckedListBox1.Items.Clear()
For m As Integer = 0 To 50
CheckedListBox1.Items.Add("Item " & m.ToString)
Next
End Sub
' --------------------------
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
For m As Integer = 0 To CheckedListBox1.Items.Count - 1
If CheckedListBox1.GetItemChecked(m) = True Then
Dim Cancel As MsgBoxResult = MsgBoxResult.Ok
Aktionen(m, Cancel)
If Cancel = MsgBoxResult.Cancel Then Exit For
End If
Next
End Sub
Sub Aktionen(ByVal Ix As Integer, ByRef Cancel As MsgBoxResult)
Select Case Ix
' ...
Case Else
Dim ant As MsgBoxResult = MsgBox("Aktion " & Ix.ToString & " noch nicht programmiert!".ToUpper, MsgBoxStyle.Information Or MsgBoxStyle.OkCancel)
Cancel = ant
End Select
End Sub
' --------------------------
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
OneCheckToggle(CheckedListBox1)
End Sub
Sub OneCheckToggle(ByVal ChLB As CheckedListBox)
Dim m As Integer = ChLB.SelectedIndex
If m = -1 Then Exit Sub
Dim chst As CheckState
Select Case ChLB.GetItemCheckState(m)
Case CheckState.Unchecked
chst = CheckState.Checked
Case CheckState.Checked
chst = CheckState.Unchecked
End Select
ChLB.SetItemCheckState(m, chst)
End Sub
' --------------------------
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
AllChecksToggle(CheckedListBox1)
End Sub
Sub AllChecksToggle(ByVal ChLB As CheckedListBox)
For m As Integer = 0 To ChLB.Items.Count - 1
Dim chst As CheckState
Select Case CheckedListBox1.GetItemCheckState(m)
Case CheckState.Unchecked
chst = CheckState.Checked
Case CheckState.Checked
chst = CheckState.Unchecked
End Select
ChLB.SetItemCheckState(m, chst)
Next
End Sub
' --------------------------
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
ClearSelected(CheckedListBox1)
End Sub
Sub ClearSelected(ByVal ChLB As CheckedListBox)
' ChLB.ClearSelected()
' alternativ
ChLB.SelectedIndex = -1
End Sub
' --------------------------
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
RemoveCheckedItems(CheckedListBox1)
End Sub
Sub RemoveCheckedItems(ByVal ChLB As CheckedListBox)
For m As Integer = ChLB.Items.Count - 1 To 0 Step -1
If ChLB.GetItemChecked(m) = True Then
ChLB.Items.RemoveAt(m)
End If
Next
End Sub
' --------------------------
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
RemoveNotCheckedItems(CheckedListBox1)
End Sub
Sub RemoveNotCheckedItems(ByVal ChLB As CheckedListBox)
For m As Integer = ChLB.Items.Count - 1 To 0 Step -1
If ChLB.GetItemChecked(m) = False Then
ChLB.Items.RemoveAt(m)
End If
Next
End Sub
Kommentare zum Snippet