List control when the user clicks the list but not on any item

Started by nandagopal, May 20, 2009, 07:19 PM

Previous topic - Next topic

nandagopal

Normally you cannot deselect all items in a ListBox once you have selected one. This example shows one way to let the user deselect all items.

When you click on an item, you see these events:

    * MouseDown
    * Click
    * MouseUp

When you click off the items, you see these events:

    * MouseDown
    * MouseUp

The trick is to determine whether you received a Click event.

In the Click event handler, set a Boolean variable indicating that a Click occurred. Then in the MouseUp event, see if it follows a Click.


Private m_ListClicked As Boolean

Private Sub List1_Click()
    m_ListClicked = True
End Sub

Private Sub List1_MouseUp(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    If Not m_ListClicked Then
        ' The user clicked off of all items.
        List1.ListIndex = -1
    Else
        m_ListClicked = False
    End If

    lblSelected.Caption = List1.Text
End Sub