KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q184206: PRB: DragDrop with ListView Control Fails in MouseMove Event

Article: Q184206
Product(s): Microsoft Visual Basic for Windows
Version(s): WINDOWS:5.0
Operating System(s): 
Keyword(s): kbVBp500 kbGrpDSVB
Last Modified: 11-JAN-2001

-------------------------------------------------------------------------------
The information in this article applies to:

- Microsoft Visual Basic Learning Edition for Windows, version 5.0 
- Microsoft Visual Basic Professional Edition for Windows, version 5.0 
- Microsoft Visual Basic Enterprise Edition for Windows, version 5.0 
-------------------------------------------------------------------------------

SYMPTOMS
========

In a ListView control, if you have DragMode set to vbManual and attempt to begin
DragDrop within the MouseMove event, the Drag operation will never begin if the
View property is set to either lvwIcon or lvwSmallIcon. The Drag operation will
begin successfully if the View property is set to lvwList or lvwReport.

CAUSE
=====

Normally, the way to initiate an ordinary DragDrop procedure is to set the Drag
property of the Listview control to vbBeginDrag. This is done by detecting the
state of the left-mouse button. If it is depressed, set the Drag property
accordingly. However, this will not work if you place your code in the MouseMove
event. When the left-mouse button is depressed, the MouseMove button never
fires, which means you can't test for the left-mouse button's state and
consequently are not able to begin the Drag operation. This is only the case
when the Listview control's View property is set to either lvwIcon or
lvwSmallIcon. If it is set to lvwList or lvwReport, the code works as expected.

RESOLUTION
==========

Add code to begin the Drag operation in the MouseDown event. The event is always
fired when the left-mouse button is clicked.

STATUS
======

Microsoft is researching this problem and will post new information here in the
Microsoft Knowledge Base as it becomes available.

MORE INFORMATION
================

Steps to Reproduce Behavior
---------------------------

1. Start with a Visual Basic form (Form1) and add four Command Buttons and two
  ListView controls.

2. Place the following code in the General Declarations section of Form1:

        Option Explicit

        Private Sub Command1_Click()
            ListView1.View = lvwIcon
        End Sub

        Private Sub Command2_Click()
            ListView1.View = lvwSmallIcon
        End Sub

        Private Sub Command3_Click()
            ListView1.View = lvwList
        End Sub

        Private Sub Command4_Click()
            ListView1.View = lvwReport
        End Sub

        Private Sub Form_Load()
            Dim Li As ListItem
            Dim colX As ColumnHeader

            Set colX = ListView1.ColumnHeaders.Add()
            colX.Text = "Field "
            colX.Width = ListView1.Width

            Set Li = ListView1.ListItems.Add(, , "hello")
            Set Li = ListView1.ListItems.Add(, , "hello2")
            Set Li = ListView1.ListItems.Add(, , "hello3")

            Command1.Caption = "lvwIcon"
            Command2.Caption = "lvwSmallIcon"
            Command3.Caption = "lvwList"
            Command4.Caption = "lvwReport"
        End Sub

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

        If Button = vbLeftButton Then
           Set ListView1.SelectedItem = ListView1.HitTest(x, y)
           ' In the following line any ICO file will do
           ListView1.DragIcon = LoadPicture( _
                "C:\program files\devstudio\vb\samples\pguide\calc\calc.ico")
           ListView1.Drag vbBeginDrag
        End If
        End Sub

        Private Sub ListView2_DragDrop(Source As Control, x As Single, _
                y As Single)
           Dim li2 As ListItem

           MsgBox "Hello"
           Set li2 = ListView2.ListItems.Add(, , Source.SelectedItem.Text)
        End Sub

3. Run the program by pressing the F5 key.

4. Try a Drag procedure with the Listview control set to lvwIcon or
  lvwSmallIcon. Note that the mouse pointer is stuck within the Listview client
  area. Change the View property to lvwList or lvwReport and note that the Drag
  operation works as expected.

Additional query words: list view drag drop drag drop kbDSupport kbDSD

======================================================================
Keywords          : kbVBp500 kbGrpDSVB 
Technology        : kbVBSearch kbAudDeveloper kbZNotKeyword6 kbZNotKeyword2 kbVB500Search kbVBA500 kbVB500
Version           : WINDOWS:5.0
Issue type        : kbprb

=============================================================================

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Copyright Microsoft Corporation 1986-2002.