Results 1 to 9 of 9

Thread: [RESOLVED] How to enable drag/drop to Listview in a Panel

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2020
    Posts
    4

    Resolved [RESOLVED] How to enable drag/drop to Listview in a Panel

    Am succesfully dragging Listview1 items to Listview2. But I cannot drag to Listview2 if I place it in a Panel. I need the Panel to group a bunch of controls. I have the standard methods as follows, as obtained from a tutorial and it works thus far but not in a Panel of course.
    Code:
     For Each c As Control In Me.Controls
                If TypeOf c Is ListView Then
                    With CType(c, ListView)
                        .AllowDrop = True
                        .FullRowSelect = True
                        .MultiSelect = False
                        AddHandler .ItemDrag, AddressOf ListView_ItemDrag
                        AddHandler .DragEnter, AddressOf ListView_DragEnter
                        AddHandler .DragDrop, AddressOf ListView_DragDrop
                    End With
    Next
    Private Sub ListView_ItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs)
            If sender Is Nothing OrElse Not TypeOf sender Is ListView Then Exit Sub
            With CType(sender, ListView)
                .DoDragDrop(e.Item, DragDropEffects.Copy)
            End With
        End Sub
    
        Private Sub ListView_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
            If sender Is Nothing OrElse Not TypeOf sender Is ListView Then Exit Sub
            'If this is a listview item then allow the drag
            If e.Data.GetDataPresent(GetType(ListViewItem)) Then
                e.Effect = DragDropEffects.Copy
            End If
        End Sub
    
        Private Sub ListView_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
            If sender Is Nothing OrElse Not TypeOf sender Is ListView Then Exit Sub
            'Remove the item from the current listview and drop it in the new listview
            With CType(sender, ListView)
                If e.Data.GetDataPresent(GetType(ListViewItem)) Then
                    Dim draggedItem As ListViewItem = CType(e.Data.GetData(GetType(ListViewItem)), ListViewItem)
                    draggedItem.ListView.Items.Remove(draggedItem)
                    .Items.Add(draggedItem)
                End If
            End With
       End Sub
    Last edited by Shaggy Hiker; Jun 29th, 2020 at 09:57 AM. Reason: Added CODE tags.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,048

    Re: How to enable drag/drop to Listview in a Panel

    You are looking in Me.Controls.

    Every control has a Controls collection. If the listview is on the form, then it is in the controls collection for the form, which is Me.Controls. If the listview is on a panel, then it is in the Controls collection of the panel: yourPanel.Controls.

    So that's the issue. You're looking in the wrong Controls collection.

    That gives you a variety of ways to solve this. Typically, people add the handlers in the designer. If you want to add them dynamically, as you are, then you generally have also created the control dynamically, in which case you might as well add the handlers in the same place where you added the control rather than doing it later the way have it.
    My usual boring signature: Nothing

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,836

    Re: How to enable drag/drop to Listview in a Panel

    If you want to add them dynamically, as you are, then you generally have also created the control dynamically, in which case you might as well add the handlers in the same place where you added the control rather than doing it later the way have it.
    Just to clarify, you don't HAVE TO create the controls to add handlers dynamically... it's just that dynamically adding handlers is in conjunction with creating the controls dynamically. That said... a third option is to add a check in your loop to check to see if there is a controls collection, and if so, if anything is in it, andd then recursively loop through the controls.

    something along these lines:
    Code:
    private sub addHandlers(controlList as List(Of Controls)) 
      for each c as Control in controlList
        If TypeOf c Is ListView Then
          With CType(c, ListView)
            .AllowDrop = True
            .FullRowSelect = True
            .MultiSelect = False
            AddHandler .ItemDrag, AddressOf ListView_ItemDrag
            AddHandler .DragEnter, AddressOf ListView_DragEnter
            AddHandler .DragDrop, AddressOf ListView_DragDrop
          End With
        else
          if c.Controls.Count >0 then
            addHandlers(c.Controls)
          end if
        end if
      next
    end sub
    Something along those lines should work... you then call it using addHandlers(me.controls)


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,004

    Re: How to enable drag/drop to Listview in a Panel

    Quote Originally Posted by techgnome View Post
    Code:
    private sub addHandlers(controlList as List(Of Controls)) 
      for each c as Control in controlList
        If TypeOf c Is ListView Then
          With CType(c, ListView)
            .AllowDrop = True
            .FullRowSelect = True
            .MultiSelect = False
            AddHandler .ItemDrag, AddressOf ListView_ItemDrag
            AddHandler .DragEnter, AddressOf ListView_DragEnter
            AddHandler .DragDrop, AddressOf ListView_DragDrop
          End With
        else
          if c.Controls.Count >0 then
            addHandlers(c.Controls)
          end if
        end if
      next
    end sub
    To clean up that method a bit:
    vb.net Code:
    1. Private Sub AddHandlers(controls As Control.ControlCollection)
    2.     For Each ctl As Control In controls
    3.         Dim lv = TryCast(ctl, ListView)
    4.  
    5.         If lv Is Nothing Then
    6.             AddHandlers(ctl.Controls)
    7.         Else
    8.             'Configure lv here.
    9.         End If
    10.     Next
    11. End Sub
    That said, there really is no point configuring them in code. If you have multiple controls on a form that you want to set a property on or handle an event of, use the Properties window. Select the multiple controls in the designer, open the Properties window and set a property. That sets it for all the selected controls. Click the Events button on the toolbar and double-click an event. That creates a single method with all the selected controls in the Handles clause.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2020
    Posts
    4

    Re: How to enable drag/drop to Listview in a Panel

    I understand now that the controls are to be found in Panel1 now, not in Form1.
    Nice.
    So, based on suggestions that this should be done more simply, I tried this through Designer instead, much less code.

    Private Sub ListView1_ItemDrag(sender As Object, e As ItemDragEventArgs) Handles ListView1.ItemDrag
    ListView1.DoDragDrop(ListView1.SelectedItems(0), DragDropEffects.Copy)
    End Sub

    Private Sub ListView1_DragEnter(sender As Object, e As DragEventArgs) Handles ListView1.DragEnter
    If e.Data.GetDataPresent(GetType(ListViewItem)) Then
    Dim item As ListViewItem = CType(e.Data.GetData("System.Windows.Forms.ListViewItem"), ListViewItem)
    ListView2.Items.Add(item.Text)
    End If
    End Sub

    Which works with the controls being in Panel1. However the drop does not feel right, it activates as soon as I barely touch Listview2. Overly quick maybe, what is the accepted norm? DragOver pastes many multiple copies of the Listview item. DragDrop does nothing.

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,048

    Re: How to enable drag/drop to Listview in a Panel

    It looks like you are doing the drop in the DragEnter method. That would certainly result in some strange behavior. I would expect that it would do exactly what you described.

    DragEnter has to be there, but it will generally be a VERY trivial method. Leave it out, and that won't be good, but it usually doesn't do much.

    DragDrop is where the dropping normally happens. You said that it does nothing, but what does that mean? Does it do nothing because you didn't implement it, or you did implement it, but it doesn't appear to be raised?
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    New Member
    Join Date
    Jun 2020
    Posts
    4

    Re: How to enable drag/drop to Listview in a Panel

    Ah, if Dragdrop is the normal method like I originally thought, then there is indeed something wrong here. Although I am taking the Listview1 item to Listview2 and it drops with DragEnter, the cursor is the circle with a diagonal line meaning 'cannot drop here'. I thought perhaps i had to change the cursor manually, but since posting this I see no indication of that being needed. And here I thought I was done :-)
    Yeah, DragDrop does not trigger. Nothing to do with the panel though, so a different question than my original.
    Last edited by ghiebert; Jun 29th, 2020 at 05:50 PM.

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,836

    Re: How to enable drag/drop to Listview in a Panel

    It's adding to Listview2 as soon as you touch it because that's what your DragEnter event handler does... that's not right... that should be in the DragDrop event.
    I think you have your events wired up wrong. ItemDrag should be against ListView1 ... DragEnter and DragDrop events should be against ListView2 .... not ListView1 as your code above shows... your original code I think was just fine... you just needed to add Handles Listview1.ItemDrag to the ItemDrag event, and Handles Listview2.DragEnter and Handles Listview2.DragDrop to the enter and drop events.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9

    Thread Starter
    New Member
    Join Date
    Jun 2020
    Posts
    4

    Re: How to enable drag/drop to Listview in a Panel

    Exactly. I shall proceed as directed.
    Anything in the panel now detects a dragdrop, using the command

    For Each c As Control In Panel1.Controls

    instead of Me.Controls. So easy.

    As far as using the Designer mainly, there were no examples that I could find anywhere, so I was doing this programmatically instead as a beginner in this particular arena. I needed a few examples so that I could familiarize myself with these types of methods but the only ones I found were fully dynamic routines. Perhaps someone could post a short tutorial on this using Designer in Visual Studio, or at least point to one that exists already.

    I am applying filtering to a Datagrid using Bindingsource, which I have never done before in that way, and I am amazed at how fast that method works, and so simply. It just turned out that dragdrop was the most intuitive solution for applying Include and Exclude arguments, and so I had to learn to implement that. I have seen this done this way in some file resizing programs, so I thought perhaps to try.
    Thanks for the help and encouragement.
    Last edited by ghiebert; Jun 30th, 2020 at 07:50 AM.

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