JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Handling Item Check Events in Checked List Boxes

When the check in front of an item in checked list box changes, an ItemCheck event occurs. The ItemCheckEventArgs object passed to the associated event handler has an Index member that gives you the index of the item whose check mark has changed, and a NewValue member that gives you the new setting for the item (which will be a member of the CheckState ennumeration: Checked, Indeterminate, or Unchecked —note that the item does not yet have this setting when you're handling this event; it will be assigned to the item after the event handler terminates). Here's how I report if an item has just gotten checked or unchecked in the CheckedListBoxes example on the CD-ROM, using this event:

Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles _
    Select Case e.NewValue
        Case CheckState.Checked
            TextBox1.Text = "Item " & e.Index + 1 & " is checked"
        Case CheckState.Unchecked
            TextBox1.Text = "Item " & e.Index + 1 & " is not checked"
    End Select
End Sub

You can see the results in Figure 7.12, where I've just unchecked item 4 in the checked list box, and the program reports that, as it should.

Figure 7.12: Responding to check events in a checked list box.

I've set CheckOnClick to True for the CheckedListBoxes example in this chapter (the default setting for this property is False), so all it takes is one click to generate an ItemCheck event. By default, however, a double-click would be needed.

Previous Section Next Section

JavaScript Editor Free JavaScript Editor     JavaScript Editor