implement drag and drop between two listviews 
Author Message
 implement drag and drop between two listviews

Hi,
  I am trying to implement drag & drop between two listview.  There is
something I am missing because it doesn't work.  I don't where where
the problem is.  Here is my code.  I hope someone could locate the
problem and tell me where I am wrong
Thank you

Private Sub ListView_DragEnter(ByVal sender As Object, _
            ByVal e As System.Windows.Forms.DragEventArgs) _
            Handles listview1.DragEnter, listview2.DragEnter

    e.Effect = e.AllowedEffect

End Sub

Private Sub ListView_ItemDrag(ByVal sender As Object, _
         ByVal e As System.Windows.Forms.ItemDragEventArgs) _
         Handles listview1.ItemDrag, listview2.ItemDrag

    DoDragDrop(e.Item, DragDropEffects.Copy)

End Sub

Private Sub ListView_DragDrop(ByVal sender As Object, _
         ByVal e As System.Windows.Forms.DragEventArgs) _
         Handles listview1.DragDrop, listview2.DragDrop

 Dim item As ListViewItem

    If e.Data.GetDataPresent("System.Windows.Forms.ListViewItem",
False) Then
    Dim pt As Point
    Dim DestinationItem As ListViewItem
    Dim s As String            
    pt = CType(sender, ListView).PointToClient(New Point(e.X, e.Y))
    DestinationItem = lvwSelectionne.GetItemAt(pt.X, pt.Y)

   item = CType(e.Data.GetData("System.Windows.Forms.ListViewItem"),
ListViewItem)

    If Not item Is Nothing Then
       If Not DestinationItem.ListView Is item.ListView Then
          DestinationItem.ListView.Items.Add(item)
       End If
    End If
End If

I hope you can help

Thank you very much



Sun, 20 Feb 2005 21:41:06 GMT  
 implement drag and drop between two listviews
Hello Martin, you can check 3 things:

1. Make sure that AllowDrop is set to true on the listview control you want
to drop onto.
2. The 'sender' argument on the DragDrop event it the control that received
the drop not the one that initiated it.
3. ListViewItems can't be part of two list view controls at the same time so
you'll need to make a copy of it.

Below is an example crafted from the code you posted:

       Private Sub ListView_DragDrop(ByVal sender As Object, _
             ByVal e As System.Windows.Forms.DragEventArgs) _
             Handles ListView1.DragDrop, ListView2.DragDrop

        If e.Data.GetDataPresent("System.Windows.Forms.ListViewItem", False)
Then
            Dim item As ListViewItem =
CType(e.Data.GetData("System.Windows.Forms.ListViewItem"), ListViewItem)
            Dim targetListView As ListView = CType(sender, ListView)
            If (Not item.ListView Is Nothing) And (Not item.ListView Is
targetListView) Then
                targetListView.Items.Add(item.Clone())
            End If
        End If
    End Sub

You'll probably want to do additional validation. For example, make sure
that the item dropped is not already in the target list view...

Hope this helps,

Antoine
Microsoft Visual Basic .NET Team

--
This posting is provided "AS IS" with no warranties, and confers no rights.


Quote:
> Hi,
>   I am trying to implement drag & drop between two listview.  There is
> something I am missing because it doesn't work.  I don't where where
> the problem is.  Here is my code.  I hope someone could locate the
> problem and tell me where I am wrong
> Thank you

> Private Sub ListView_DragEnter(ByVal sender As Object, _
>             ByVal e As System.Windows.Forms.DragEventArgs) _
>             Handles listview1.DragEnter, listview2.DragEnter

>     e.Effect = e.AllowedEffect

> End Sub

> Private Sub ListView_ItemDrag(ByVal sender As Object, _
>          ByVal e As System.Windows.Forms.ItemDragEventArgs) _
>          Handles listview1.ItemDrag, listview2.ItemDrag

>     DoDragDrop(e.Item, DragDropEffects.Copy)

> End Sub

> Private Sub ListView_DragDrop(ByVal sender As Object, _
>          ByVal e As System.Windows.Forms.DragEventArgs) _
>          Handles listview1.DragDrop, listview2.DragDrop

>  Dim item As ListViewItem

>     If e.Data.GetDataPresent("System.Windows.Forms.ListViewItem",
> False) Then
>     Dim pt As Point
>     Dim DestinationItem As ListViewItem
>     Dim s As String
>     pt = CType(sender, ListView).PointToClient(New Point(e.X, e.Y))
>     DestinationItem = lvwSelectionne.GetItemAt(pt.X, pt.Y)

>    item = CType(e.Data.GetData("System.Windows.Forms.ListViewItem"),
> ListViewItem)

>     If Not item Is Nothing Then
>        If Not DestinationItem.ListView Is item.ListView Then
>           DestinationItem.ListView.Items.Add(item)
>        End If
>     End If
> End If

> I hope you can help

> Thank you very much



Tue, 22 Feb 2005 04:50:56 GMT  
 implement drag and drop between two listviews
Thank you for you answer..

The code you did provide almost worked.  I have to change one line.
The line :
targetListView.Items.Add(item.Clone())
return the syntax error : Overload resolution failed because no
accessible 'Add' can be called with these arguments.

So I change this line for :
targetListView.Items.Add(item.SubItems(0).Text)
and it works

And now, I want to implement multi selection drag & drop but the item
object only contains one row.  How can I implement multiple selection
drag & drop from one listview to another listview??

Thank you for your help, it's very appreciated.

P.S.  Antoine, you have a french name, do you have french origin?


Quote:
> Hello Martin, you can check 3 things:

> 1. Make sure that AllowDrop is set to true on the listview control you want
> to drop onto.
> 2. The 'sender' argument on the DragDrop event it the control that received
> the drop not the one that initiated it.
> 3. ListViewItems can't be part of two list view controls at the same time so
> you'll need to make a copy of it.

> Below is an example crafted from the code you posted:

>        Private Sub ListView_DragDrop(ByVal sender As Object, _
>              ByVal e As System.Windows.Forms.DragEventArgs) _
>              Handles ListView1.DragDrop, ListView2.DragDrop

>         If e.Data.GetDataPresent("System.Windows.Forms.ListViewItem", False)
> Then
>             Dim item As ListViewItem =
> CType(e.Data.GetData("System.Windows.Forms.ListViewItem"), ListViewItem)
>             Dim targetListView As ListView = CType(sender, ListView)
>             If (Not item.ListView Is Nothing) And (Not item.ListView Is
> targetListView) Then
>                 targetListView.Items.Add(item.Clone())
>             End If
>         End If
>     End Sub

> You'll probably want to do additional validation. For example, make sure
> that the item dropped is not already in the target list view...

> Hope this helps,

> Antoine
> Microsoft Visual Basic .NET Team

> --
> This posting is provided "AS IS" with no warranties, and confers no rights.



> > Hi,
> >   I am trying to implement drag & drop between two listview.  There is
> > something I am missing because it doesn't work.  I don't where where
> > the problem is.  Here is my code.  I hope someone could locate the
> > problem and tell me where I am wrong
> > Thank you

> > Private Sub ListView_DragEnter(ByVal sender As Object, _
> >             ByVal e As System.Windows.Forms.DragEventArgs) _
> >             Handles listview1.DragEnter, listview2.DragEnter

> >     e.Effect = e.AllowedEffect

> > End Sub

> > Private Sub ListView_ItemDrag(ByVal sender As Object, _
> >          ByVal e As System.Windows.Forms.ItemDragEventArgs) _
> >          Handles listview1.ItemDrag, listview2.ItemDrag

> >     DoDragDrop(e.Item, DragDropEffects.Copy)

> > End Sub

> > Private Sub ListView_DragDrop(ByVal sender As Object, _
> >          ByVal e As System.Windows.Forms.DragEventArgs) _
> >          Handles listview1.DragDrop, listview2.DragDrop

> >  Dim item As ListViewItem

> >     If e.Data.GetDataPresent("System.Windows.Forms.ListViewItem",
> > False) Then
> >     Dim pt As Point
> >     Dim DestinationItem As ListViewItem
> >     Dim s As String
> >     pt = CType(sender, ListView).PointToClient(New Point(e.X, e.Y))
> >     DestinationItem = lvwSelectionne.GetItemAt(pt.X, pt.Y)

> >    item = CType(e.Data.GetData("System.Windows.Forms.ListViewItem"),
> > ListViewItem)

> >     If Not item Is Nothing Then
> >        If Not DestinationItem.ListView Is item.ListView Then
> >           DestinationItem.ListView.Items.Add(item)
> >        End If
> >     End If
> > End If

> > I hope you can help

> > Thank you very much



Tue, 22 Feb 2005 23:45:47 GMT  
 implement drag and drop between two listviews
Hello Martin,

you got a compiler error with the code that was posted yesterday because
Option Strict was set to On in your project.

If you want to implement multi selection drag&drop, first make sure that the
MultiSelect property of the ListView control is set to true. Then when you
call DoDragDrop you could pass the ListView's SelectedItems property instead
of passing in a single item. When you handle the drop you can then insert
each item in the collection into the target ListView control

Hope this helps,
Antoine & Abel
Microsoft Visual Basic .Net Team

--
This posting is provided "AS IS" with no warranties, and confers no rights.


Quote:
> Thank you for you answer..

> The code you did provide almost worked.  I have to change one line.
> The line :
> targetListView.Items.Add(item.Clone())
> return the syntax error : Overload resolution failed because no
> accessible 'Add' can be called with these arguments.

> So I change this line for :
> targetListView.Items.Add(item.SubItems(0).Text)
> and it works

> And now, I want to implement multi selection drag & drop but the item
> object only contains one row.  How can I implement multiple selection
> drag & drop from one listview to another listview??

> Thank you for your help, it's very appreciated.

> P.S.  Antoine, you have a french name, do you have french origin?




- Show quoted text -

Quote:
> > Hello Martin, you can check 3 things:

> > 1. Make sure that AllowDrop is set to true on the listview control you
want
> > to drop onto.
> > 2. The 'sender' argument on the DragDrop event it the control that
received
> > the drop not the one that initiated it.
> > 3. ListViewItems can't be part of two list view controls at the same
time so
> > you'll need to make a copy of it.

> > Below is an example crafted from the code you posted:

> >        Private Sub ListView_DragDrop(ByVal sender As Object, _
> >              ByVal e As System.Windows.Forms.DragEventArgs) _
> >              Handles ListView1.DragDrop, ListView2.DragDrop

> >         If e.Data.GetDataPresent("System.Windows.Forms.ListViewItem",
False)
> > Then
> >             Dim item As ListViewItem =
> > CType(e.Data.GetData("System.Windows.Forms.ListViewItem"), ListViewItem)
> >             Dim targetListView As ListView = CType(sender, ListView)
> >             If (Not item.ListView Is Nothing) And (Not item.ListView Is
> > targetListView) Then
> >                 targetListView.Items.Add(item.Clone())
> >             End If
> >         End If
> >     End Sub

> > You'll probably want to do additional validation. For example, make sure
> > that the item dropped is not already in the target list view...

> > Hope this helps,

> > Antoine
> > Microsoft Visual Basic .NET Team

> > --
> > This posting is provided "AS IS" with no warranties, and confers no
rights.



> > > Hi,
> > >   I am trying to implement drag & drop between two listview.  There is
> > > something I am missing because it doesn't work.  I don't where where
> > > the problem is.  Here is my code.  I hope someone could locate the
> > > problem and tell me where I am wrong
> > > Thank you

> > > Private Sub ListView_DragEnter(ByVal sender As Object, _
> > >             ByVal e As System.Windows.Forms.DragEventArgs) _
> > >             Handles listview1.DragEnter, listview2.DragEnter

> > >     e.Effect = e.AllowedEffect

> > > End Sub

> > > Private Sub ListView_ItemDrag(ByVal sender As Object, _
> > >          ByVal e As System.Windows.Forms.ItemDragEventArgs) _
> > >          Handles listview1.ItemDrag, listview2.ItemDrag

> > >     DoDragDrop(e.Item, DragDropEffects.Copy)

> > > End Sub

> > > Private Sub ListView_DragDrop(ByVal sender As Object, _
> > >          ByVal e As System.Windows.Forms.DragEventArgs) _
> > >          Handles listview1.DragDrop, listview2.DragDrop

> > >  Dim item As ListViewItem

> > >     If e.Data.GetDataPresent("System.Windows.Forms.ListViewItem",
> > > False) Then
> > >     Dim pt As Point
> > >     Dim DestinationItem As ListViewItem
> > >     Dim s As String
> > >     pt = CType(sender, ListView).PointToClient(New Point(e.X, e.Y))
> > >     DestinationItem = lvwSelectionne.GetItemAt(pt.X, pt.Y)

> > >    item = CType(e.Data.GetData("System.Windows.Forms.ListViewItem"),
> > > ListViewItem)

> > >     If Not item Is Nothing Then
> > >        If Not DestinationItem.ListView Is item.ListView Then
> > >           DestinationItem.ListView.Items.Add(item)
> > >        End If
> > >     End If
> > > End If

> > > I hope you can help

> > > Thank you very much



Wed, 23 Feb 2005 03:55:26 GMT  
 implement drag and drop between two listviews
Well..

  I have some difficulties.  So, if I understand well, I got to pass
the selected items so in the ItemDrag event I do :

DoDragDrop(CType(sender, ListView).SelectedItems,
DragDropEffects.Move)

instead of

DoDragDrop(e.Item, DragDropEffects.Move)

but in the DragDrop, I know I have something to do with the
iEnumerable interface but I can't figure how to use it.  Here is my
actual DragDrop event code managing a single item.  What should I do
to manage the selecteditems??

Private Sub List_DragDrop(ByVal sender As Object, _
               ByVal e As System.Windows.Forms.DragEventArgs) _
               Handles listview1.DragDrop, listview2.DragDrop

   Dim dropItem As ListViewItem =
CType(e.Data.GetData("System.Windows.Forms.ListViewItem"),
ListViewItem)
   Dim dragItem As ListViewItem =
CType(e.Data.GetData(dropItem.GetType()), ListViewItem)

   Dim targetListView As ListView = CType(sender, ListView)

   If (Not dragItem.ListView Is Nothing) And _
      (Not dragItem.ListView Is targetListView) Then
       dragItem.Remove()
       targetListView.Items.Add(dragItem)
    End If

You help will be very appreciated.

Thank you


Quote:
> Hello Martin,

> you got a compiler error with the code that was posted yesterday because
> Option Strict was set to On in your project.

> If you want to implement multi selection drag&drop, first make sure that the
> MultiSelect property of the ListView control is set to true. Then when you
> call DoDragDrop you could pass the ListView's SelectedItems property instead
> of passing in a single item. When you handle the drop you can then insert
> each item in the collection into the target ListView control

> Hope this helps,
> Antoine & Abel
> Microsoft Visual Basic .Net Team

> --
> This posting is provided "AS IS" with no warranties, and confers no rights.



Fri, 25 Feb 2005 09:35:18 GMT  
 implement drag and drop between two listviews
VbDotTeam...can you help me please ?
Quote:

> Well..

>   I have some difficulties.  So, if I understand well, I got to pass
> the selected items so in the ItemDrag event I do :

> DoDragDrop(CType(sender, ListView).SelectedItems,
> DragDropEffects.Move)

> instead of

> DoDragDrop(e.Item, DragDropEffects.Move)

> but in the DragDrop, I know I have something to do with the
> iEnumerable interface but I can't figure how to use it.  Here is my
> actual DragDrop event code managing a single item.  What should I do
> to manage the selecteditems??

> Private Sub List_DragDrop(ByVal sender As Object, _
>                ByVal e As System.Windows.Forms.DragEventArgs) _
>                Handles listview1.DragDrop, listview2.DragDrop

>    Dim dropItem As ListViewItem =
> CType(e.Data.GetData("System.Windows.Forms.ListViewItem"),
> ListViewItem)
>    Dim dragItem As ListViewItem =
> CType(e.Data.GetData(dropItem.GetType()), ListViewItem)

>    Dim targetListView As ListView = CType(sender, ListView)

>    If (Not dragItem.ListView Is Nothing) And _
>       (Not dragItem.ListView Is targetListView) Then
>        dragItem.Remove()
>        targetListView.Items.Add(dragItem)
>     End If

> You help will be very appreciated.

> Thank you


> > Hello Martin,

> > you got a compiler error with the code that was posted yesterday because
> > Option Strict was set to On in your project.

> > If you want to implement multi selection drag&drop, first make sure that the
> > MultiSelect property of the ListView control is set to true. Then when you
> > call DoDragDrop you could pass the ListView's SelectedItems property instead
> > of passing in a single item. When you handle the drop you can then insert
> > each item in the collection into the target ListView control

> > Hope this helps,
> > Antoine & Abel
> > Microsoft Visual Basic .Net Team

> > --
> > This posting is provided "AS IS" with no warranties, and confers no rights.



Sun, 27 Feb 2005 22:15:41 GMT  
 implement drag and drop between two listviews
Hi, Martin

I figured out a way to do this.  It may not be the best way, but it has the
virture of working.

What I did was iterate through the SelectedItems collection in the ItemDrag
event as follows:

      Dim lv As ListView = CType(sender, ListView)
      Dim lvi As ListViewItem

      ' This code iterates throught the selected items collection, dropping
each item on target
      For Each lvi In lv.SelectedItems
         DoDragDrop(lvi, DragDropEffects.Copy)
      Next

The code in the DragDrop event remained unchanged.

If I discover a better way, I will post it here.

HTH,

John Eikanger
Microsoft Visual Basic Team

This posting is provided AS IS with no warranties, and confers no rights.
(c) 2002 Microsoft Corporation.  All rights reserved.
--------------------

| Newsgroups: microsoft.public.dotnet.languages.vb
| Subject: Re: implement drag and drop between two listviews
| Date: 11 Sep 2002 07:15:41 -0700
| Organization: http://groups.google.com/
| Lines: 64






| NNTP-Posting-Host: 206.162.131.219
| Content-Type: text/plain; charset=ISO-8859-1
| Content-Transfer-Encoding: 8bit
| X-Trace: posting.google.com 1031753742 3781 127.0.0.1 (11 Sep 2002
14:15:42 GMT)

| NNTP-Posting-Date: 11 Sep 2002 14:15:42 GMT
| Path:
cpmsftngxa08!cpmsftngxa07!tkmsftngp01!newsfeed00.sul.t-online.de!t-online.de
!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed-east.nntpserver.co
m!nntpserver.com!newsfeed1.easynews.com!easynews.com!easynews!sn-xit-02!sn-x
it-01!sn-xit-04!supernews.com!postnews1.google.com!not-for-mail
| Xref: cpmsftngxa08 microsoft.public.dotnet.languages.vb:64227
| X-Tomcat-NG: microsoft.public.dotnet.languages.vb
|
| VbDotTeam...can you help me please ?
|
|
|

| > Well..
| >
| >   I have some difficulties.  So, if I understand well, I got to pass
| > the selected items so in the ItemDrag event I do :
| >
| > DoDragDrop(CType(sender, ListView).SelectedItems,
| > DragDropEffects.Move)
| >
| > instead of
| >
| > DoDragDrop(e.Item, DragDropEffects.Move)
| >
| > but in the DragDrop, I know I have something to do with the
| > iEnumerable interface but I can't figure how to use it.  Here is my
| > actual DragDrop event code managing a single item.  What should I do
| > to manage the selecteditems??
| >
| > Private Sub List_DragDrop(ByVal sender As Object, _
| >                ByVal e As System.Windows.Forms.DragEventArgs) _
| >                Handles listview1.DragDrop, listview2.DragDrop
| >
| >    Dim dropItem As ListViewItem =
| > CType(e.Data.GetData("System.Windows.Forms.ListViewItem"),
| > ListViewItem)
| >    Dim dragItem As ListViewItem =
| > CType(e.Data.GetData(dropItem.GetType()), ListViewItem)
| >
| >    Dim targetListView As ListView = CType(sender, ListView)
| >
| >    If (Not dragItem.ListView Is Nothing) And _
| >       (Not dragItem.ListView Is targetListView) Then
| >        dragItem.Remove()
| >        targetListView.Items.Add(dragItem)
| >     End If
| >
| >
| > You help will be very appreciated.
| >
| > Thank you
| >


| > > Hello Martin,
| > >
| > > you got a compiler error with the code that was posted yesterday
because
| > > Option Strict was set to On in your project.
| > >
| > > If you want to implement multi selection drag&drop, first make sure
that the
| > > MultiSelect property of the ListView control is set to true. Then
when you
| > > call DoDragDrop you could pass the ListView's SelectedItems property
instead
| > > of passing in a single item. When you handle the drop you can then
insert
| > > each item in the collection into the target ListView control
| > >
| > > Hope this helps,
| > > Antoine & Abel
| > > Microsoft Visual Basic .Net Team
| > >
| > > --
| > > This posting is provided "AS IS" with no warranties, and confers no
rights.
| > >
|



Tue, 01 Mar 2005 03:45:57 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Drag and Drop images between two listviews

2. Drop/Drop between two listviews

3. Problem with implementing drag and drop

4. Drag and Drop between two Treeview controls?

5. Drag&Drop between two forms

6. Drag and drop between two treeview

7. how can I drag and drop treeview nodes between two instances of a single tree control

8. OLE Drag and Drop between two treeviews

9. Dragging and Dropping from two instances of an app

10. Drag and Drop between two Treeview controls?

11. how can I drag and drop treeview nodes between two instances of a single tree control

12. I Dont get the Listview drag and drop model

 

 
Powered by phpBB® Forum Software