Microsoft KB Archive/240946

= BUG: ListView Fails to Raise Mouse Events When MultiSelect Is Set to True =

Article ID: 240946

Article Last Modified on 5/12/2003

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q240946



SYMPTOMS
If the MultiSelect property of the Mscomctl.ocx ListView control is set to True, and the bounding box (marquee) selection has begun (the left- or right-mouse button is depressed and dragged in the ListView client window, and not on a ListItem), the ListView does not receive the MouseUp and Click events when the mouse button is released.



RESOLUTION
To work around this problem, use a module-level variable and the MouseMove event.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.



Steps to Reproduce Behavior
 Create a new Standard EXE project in Visual Basic. Form1 is created by default. From the Project menu, select Components, and add a reference to Microsoft Windows Common Controls 6.0. Place a ListView control on Form1.  Paste the following code into the code window of Form1: Option Explicit

Private Sub Form_Load Dim i As Integer

ListView1.MultiSelect = True

For i = 1 To 20 Call ListView1.ListItems.Add(Text:="item" & i) Next

End Sub

Private Sub ListView1_MouseDown(Button As Integer, _           Shift As Integer, x As Single, y As Single)

Debug.Print "MouseDown"; Button; Shift; x; y

End Sub

Private Sub ListView1_MouseUp(Button As Integer, _           Shift As Integer, x As Single, y As Single)

Debug.Print "MouseUp"; Button; Shift; x; y

End Sub

Private Sub ListView1_Click Debug.Print "Click" End Sub  Press the F5 key to run the program. From the View menu, select the Immediate window.</li> Left-click or right-click on a place inside the ListView control (not on an item), drag the selection box, and then release the mouse button.</li> In the Immediate window, note that the MouseDown event is fired, but not the MouseUp event or the Click event.</li></ol>

Steps to Workaround This Problem
<ol> Create a new Standard EXE project in Visual Basic. Form1 is created by default.</li> On the Project menu, select Components, and add a reference to Microsoft Windows Common Controls 6.0.</li> Place a ListView control on Form1.</li>  Paste the following code into the code window of Form1: ' Index of currently depressed button (vbLeftButton or vbRightButton) Private m_iButton As Integer

Private Sub Form_Load Dim i As Integer

ListView1.MultiSelect = True

For i = 1 To 20 Call ListView1.ListItems.Add(Text:="item" & i) Next

End Sub

Private Sub ListView1_MouseDown(Button As Integer, _           Shift As Integer, x As Single, y As Single)

Debug.Print "MouseDown"; Button; Shift; x; y

' If the left- or right-mouse button is being depressed, and is not over ' a ListItem, set the module level variable to the button's value. If Button And (ListView1.HitTest(x, y) Is Nothing) Then m_iButton = Button End If

End Sub

Private Sub ListView1_MouseMove(Button As Integer, _           Shift As Integer, x As Single, y As Single)

' Debug.Print "MouseMove"; Button; Shift; x; y

' If no mouse button is currently pressed, and the module level ' variable still contains a button value set in MouseDown, then ' a mouse button is being released, call the MouseUp and Click ' events, and zero the variable. (to our favor, the ListView raises ' a MouseMove when any mouse button is released). If (Button = 0) And m_iButton Then Call ListView1_MouseUp(m_iButton, Shift, x, y)   Call ListView1_Click m_iButton = 0 End If

End Sub

Private Sub ListView1_MouseUp(Button As Integer, _           Shift As Integer, x As Single, y As Single)

Debug.Print "MouseUp"; Button; Shift; x; y

' Clear the module level variable here also in case one mouse button ' is depressed while the other is already down, and either is released, ' prevents the MouseMove code from calling unwanted events m_iButton = 0

End Sub

Private Sub ListView1_Click Debug.Print "Click" End Sub </li> Press F5 to run the program.</li> On the View, select the Immediate window.</li> Left-click or right-click on a place inside the ListView control (not on an item), drag the selection box, and then release the mouse button.</li> In the Immediate window, note that all three of the events are fired.</li></ol>

Additional query words: sp4

Keywords: kbbug kblistview kbpending KB240946

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.