No ItemChecked event in a CheckedListBox?

后端 未结 4 1129
故里飘歌
故里飘歌 2020-11-29 08:53

The ListView control has an ItemCheck event which is fired before the item changes, and an ItemChecked event that is fired aft

4条回答
  •  隐瞒了意图╮
    2020-11-29 09:41

    I use a Timer to solve this problem. Enable the timer via the ItemCheck event. Take action in the Timer's Tick event.

    This works whether the item is checked via a mouse click or by pressing the Space-Bar. We'll take advantage of the fact that the item just checked (or un-checked) is always the Selected Item.

    The Timer's Interval can be as low as 1. By the time the Tick event is raised, the new Checked status will be set.

    This VB.NET code shows the concept. There are many variations you can employ. You may want to increase the Timer's Interval to allow the user to change the check status on several items before taking action. Then in the Tick event, make a sequential pass of all the

    Items in the List or use its CheckedItems collection to take appropriate action.

    That's why we first disable the Timer in the ItemCheck event. Disable then Enable causes the Interval period to re-start.

    Private Sub ckl_ItemCheck(ByVal sender As Object, _
                              ByVal e As System.Windows.Forms.ItemCheckEventArgs) _
            Handles ckl.ItemCheck
    
    tmr.Enabled = False
    tmr.Enabled = True
    
    End Sub
    
    
    Private Sub tmr_Tick(ByVal sender As System.Object, _
                         ByVal e As System.EventArgs) _
    Handles tmr.Tick
    
    tmr.Enabled = False
    Debug.Write(ckl.SelectedIndex)
    Debug.Write(": ")
    Debug.WriteLine(ckl.GetItemChecked(ckl.SelectedIndex).ToString)
    
    End Sub
    

提交回复
热议问题