Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Drop items on specific location in listview

Drop items on specific location in listview

Scheduled Pinned Locked Moved Visual Basic
databasebeta-testingcode-review
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Noctris
    wrote on last edited by
    #1

    Hi, I've been messing with this for a while and am obviously missing something but i just don't see it :s... I'm trying to do a drag drop between 2 listviews and want to drop on a specific location.. Although i think my code is right (which it obviously isn't :p) .. it always returns index -1 on the droplocation.. my code:

    Private Sub LVPlaylist_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles LVPlaylist.DragOver
    ' Retrieve the client coordinates of the mouse pointer.
    Dim targetPoint As Point = LVPlaylist.PointToClient(New Point(e.X, e.Y))

        ' Retrieve the index of the item closest to the mouse pointer.
        Dim targetIndex As Integer = \_
            LVPlaylist.InsertionMark.NearestIndex(targetPoint)
    
    
        'just some visual feedback to see where I am dragging.. -1 always anyways..
        LblTargetIndex.Text = LVPlaylist.GetItemAt(targetPoint.X, targetPoint.Y).ToString
        ' Confirm that the mouse pointer is not over the dragged item.
        If targetIndex > -1 Then
            ' Determine whether the mouse pointer is to the left or
            ' the right of the midpoint of the closest item and set
            ' the InsertionMark.AppearsAfterItem property accordingly.
            Dim itemBounds As Rectangle = LVPlaylist.GetItemRect(targetIndex)
            If targetPoint.X > itemBounds.Left + (itemBounds.Width / 2) Then
                LVPlaylist.InsertionMark.AppearsAfterItem = True
            Else
                LVPlaylist.InsertionMark.AppearsAfterItem = False
            End If
        End If
    
        ' Set the location of the insertion mark. If the mouse is
        ' over the dragged item, the targetIndex value is -1 and
        ' the insertion mark disappears.
        LVPlaylist.InsertionMark.Index = targetIndex
    
    End Sub
    

    Private Sub LVPlaylist_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles LVPlaylist.DragDrop
    Dim MyItem As ListViewItem
    Dim MyItems() As ListViewItem = e.Data.GetData("System.Windows.Forms.ListviewItem()")
    Dim i As Integer = 0
    Dim targetIndex As Integer = LVPlaylist.InsertionMark.Index
    For Each MyItem In LVfiles.SelectedItems
    'is -1 ALWAYS so put it at the top...
    If targetindex = -1 Then targetindex = 0
    'TODO: make sure the targetindex get's ++ for every item dropped
    Dim newitem As ListViewItem = MyItem.Clone
    LVPlaylist.Items.Insert

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups