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
 
 
 
 
 
TitleTell if a ListBox's value changed because of a mouse click or code
KeywordsListBox, click or code
CategoriesControls
 
The ListBox fires its Click event when the user clicks on an item or when the program sets its value as in:

lstAnimals.ListIndex = 1

To tell which is the case, create a module-level Boolean variable m_MouseClicking. In the ListBox's MouseDown event handler, set this value to True. Check the value in the Click event handler. In the ListBox's MouseUp event handler, set this value back to False.

 
Private m_MouseClicking As Boolean

Private Sub lstAnimals_MouseDown(Button As Integer, Shift _
    As Integer, X As Single, Y As Single)
    m_MouseClicking = True
End Sub

Private Sub lstAnimals_MouseUp(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    m_MouseClicking = False
End Sub

Private Sub lstAnimals_Click()
    If m_MouseClicking Then
        lblSource.Caption = "Mouse Click"
    Else
        lblSource.Caption = "Code"
    End If
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated