Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleClear a List control when the user clicks the list but not on any item
KeywordsListBox, clear list, click off list
CategoriesControls
 
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
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated