Solved

VB.net Drag and Drop and Mouse Move

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error handling in asp.net site 5 34
Expression Evaluater 3 38
How to duplicate form in Visual Studio 2015 2 61
Need syntax help building hyperlink 2 27
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

829 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