Solved

VB.net Drag and Drop and Mouse Move

Posted on 2008-10-22
4
881 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 86

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

738 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