Solved

VB.net Drag and Drop and Mouse Move

Posted on 2008-10-22
4
879 Views
Last Modified: 2010-04-21
Hi in the event  "lsvAction_DragDrop" I need to change the line that the user drags to using the following event. The problem is that it doesn't work all the time. I need to get the exact index before the DragDrop is initiated
Private Sub lsvAction_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lsvAction.MouseMove

        Dim LocalMousePosition As Point
        LocalMousePosition = Me.lsvAction.PointToClient(Cursor.Position)

        Dim x As Integer = LocalMousePosition.X
        Dim y As Integer = LocalMousePosition.Y

        Dim HoveredItem As ListViewItem = Me.lsvAction.GetItemAt(x, y)
        If HoveredItem Is Nothing Then Return

        ListIndex_on_Mousehover = HoveredItem.Index

    End Sub
0
Comment
Question by:murbro
  • 2
4 Comments
 
LVL 10

Expert Comment

by:kdwood
ID: 22781287
Why not use the MouseDown event.  That way you know for sure what the index of the item is that they clicked on to drag.  For example I have an application where I'm dragging items between two ListBoxes.  I approach it as follows:

1. On the ListBox MouseDown Event:

                If Me.YourListBoxDraggedFrom.SelectedIndex <> -1 Then
                    Dim dragData As String = Me.YourListBox.SelectedItem
                    Me.YourListBox.DoDragDrop(dragData, DragDropEffects.Copy)
                End If

2. On the ListBox DragLeaveEvent:

     e.Effect = DragDropEffects.Copy

3. On the Listbox DragDropEvent

     Me.YourListboxDraggedTo.Items.Add(e.Data.GetData(GetType(String)))

Let me know if this helps.

Regards,

Keith

0
 
LVL 10

Accepted Solution

by:
kdwood earned 250 total points
ID: 22781306
oops typo.... section 1 should read:

1. On the ListBox MouseDown Event:

                If Me.YourListBoxDraggedFrom.SelectedIndex <> -1 Then
                    Dim dragData As String = Me.YourListBox.SelectedItem
                    Me.YourListBoxDraggedFrom.DoDragDrop(dragData, DragDropEffects.Copy)
                End If
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 250 total points
ID: 22781473
You should get multiple DragOver() events before the final DragDrop() event.

Try:
    Private Sub lsvAction_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lsvAction.DragOver        
        Dim HoveredItem As ListViewItem = Me.lsvAction.GetItemAt(e.X, e.Y)
        If Not IsNothing(HoveredItem) Then
            ListIndex_on_Mousehover = HoveredItem.Index
        End If
    End Sub

Open in new window

0
 

Author Closing Comment

by:murbro
ID: 31508934
thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help with consolidating excel files using VB.net 2 35
Winform Module - What is the ASP.Net equiv 2 38
"Emulate" TAB key when press Enter Key 3 66
Name Space error VS2015 1 35
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question