Solved

VB.net Drag and Drop and Mouse Move

Posted on 2008-10-22
4
866 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now