Results 1 to 2 of 2

Thread: listview drag drop handling [resolved]

  1. #1

    Thread Starter
    Addicted Member Sibby's Avatar
    Join Date
    Feb 2001
    Location
    Milwaukee, WI *The United States of America*
    Posts
    144

    listview drag drop handling [resolved]

    I'm wondering if anyone's got any working samples of dragging and dropping items and subitems to and from listviews. What I'm trying to do specifically is drag an item from a listbox to a listview 's item text.

    I would think this would be easy to do with the GetItemAt method, but I can't get it to work. Any help would be appreciated.

    Sibby
    Last edited by Sibby; Feb 3rd, 2004 at 05:41 PM.
    If you can think it....you can code it....

  2. #2

    Thread Starter
    Addicted Member Sibby's Avatar
    Join Date
    Feb 2001
    Location
    Milwaukee, WI *The United States of America*
    Posts
    144

    Talking After a day of on and off putzing....

    I finally got this working how I want it. To get a sample of this working for yourself, add a listview and listbox control to your form.

    In my case I wasn't worried about dropping into a specific subitem, but this should give anyone else a start to getting that functionality working as well.

    VB Code:
    1. Dim iSelectedIndex As Integer
    2.     Dim iLastItemIndex As Integer
    3.  
    4.     Private Sub Listview1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Listview1.DragDrop
    5.         'get the text from our dropped item and assign it to the hovered item's text property
    6.         Dim sData As String = e.Data.GetData(DataFormats.Text).ToString()
    7.  
    8.         Listview1.Items(iLastItemIndex).Text = sData
    9.     End Sub
    10.  
    11.     Private Sub Listbox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Listbox1.MouseMove
    12.         'if holding down left mouse, start our dragdrop by passing the selected index from the listbox
    13.         If e.Button = MouseButtons.Left Then
    14.             Listbox1.DoDragDrop(Listbox1.Items(iSelectedIndex), DragDropEffects.Copy)
    15.         End If
    16.     End Sub
    17.  
    18.     Private Sub Listview1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Listview1.DragEnter
    19.         'set the drag effect based on our drag data
    20.         If e.Data.GetDataPresent(DataFormats.Text) Then
    21.             e.Effect = DragDropEffects.Copy
    22.         Else
    23.             e.Effect = DragDropEffects.None
    24.         End If
    25.     End Sub
    26.  
    27.     Private Sub Listbox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Listbox1.MouseDown
    28.         'save off the current selection because it doesn't get set in the mousemove or click event
    29.         iSelectedIndex = Listbox1.SelectedIndex
    30.     End Sub
    31.  
    32.     Private Sub Listview1_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Listview1.DragOver
    33.         'find the currently hovered item, select it and save it off
    34.         Dim cp As Point = Listview1.PointToClient(New Point(e.X, e.Y))
    35.         Dim hoverItem As ListViewItem = Listview1.GetItemAt(cp.X, cp.Y)
    36.  
    37.         If Not (hoverItem Is Nothing) Then
    38.             e.Effect = DragDropEffects.Copy
    39.             iLastItemIndex = hoverItem.Index
    40.             Listview1.Items(iLastItemIndex).Selected = True
    41.         Else
    42.             e.Effect = DragDropEffects.None
    43.         End If
    44.     End Sub

    Sibby
    If you can think it....you can code it....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width