Click here to Skip to main content
16,014,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi All

i have 5 different Listview and one treeview .

the treeview include 5 different nodes , each node show one of the Listview .

node1 show > Listview1

node2 show > Listview2

node3 show > Listview3

node4 show > Listview4

node5 show > Listview5

what i need now to drag and drop the selected item from one of the listview to another by drop it over specific node , so the selected item moved to the listview that represented by the node.

example : if i select the itemX from listview3 and drop it over node 5 >> the result should be >> the itemx moved from listview3 to listview5.

Microsoft Visual Basic 2008 Express Edition

thanks in advance
Posted

Assuming that:
1. Your ListView components have names: ListView1, ListView2...ListView5
2. Your TreeView component is named: TreeView1
3. Your TreeView Nodes Text property holds name of destination ListView

VB
Private sourceListView As ListView

Private Sub ListView_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) _
    Handles ListView1.MouseDown, ListView5.MouseDown, ListView4.MouseDown, ListView3.MouseDown, ListView2.MouseDown
    ' Store source ListView for Drag & Drop
    sourceListView = CType(sender, ListView)
End Sub

Private Sub ListView_DragOver(sender As System.Object, e As System.Windows.Forms.DragEventArgs) _
    Handles ListView1.DragOver, ListView5.DragOver, ListView4.DragOver, ListView3.DragOver, ListView2.DragOver
    e.Effect = DragDropEffects.Move
End Sub

Private Sub ListView_ItemDrag(sender As System.Object, e As System.Windows.Forms.ItemDragEventArgs) _
    Handles ListView1.ItemDrag, ListView5.ItemDrag, ListView4.ItemDrag, ListView3.ItemDrag, ListView2.ItemDrag
    ' Set selected item Text to Drag & Drop operation
    sourceListView.DoDragDrop(sourceListView.SelectedItems(0).Text, DragDropEffects.Move)
End Sub

Private Sub TreeView_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) _
    Handles TreeView1.DragEnter
    e.Effect = DragDropEffects.Move
End Sub

Private Sub TreeView_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) _
    Handles TreeView1.DragDrop
    Dim destNode As TreeNode

    ' Get destinated TreeView Node
    destNode = TreeView1.GetNodeAt(TreeView1.PointToClient(New Point(e.X, e.Y)))

    If IsNothing(destNode) Then
        Return
    End If

    ' Gets dragged text
    Dim itemText As String
    itemText = DirectCast(e.Data.GetData(DataFormats.StringFormat), String)

    ' Assuming Node.Tag contains name of destinated ListView...
    Dim destListViewName As String
    destListViewName = destNode.Text

    ' ...find it on Form
    Dim destListView As ListView
    destListView = CType(Me.Controls.Find(destListViewName, True)(0), ListView)

    ' Add item to destinated ListView...
    destListView.Items.Add(itemText)

    ' ... and remove it from source ListView
    sourceListView.Items.Remove(sourceListView.FindItemWithText(itemText))
End Sub


I hope you find this solution usefull :)
 
Share this answer
 
v2
Comments
Member 10491813 17-Mar-14 6:36am    
Thanks Marcin
can you tell me what should change in code to move the subitems also , it's my bad i didn't mention the items have 2 subitems .
Marcin Kozub 17-Mar-14 6:52am    
Just replace some code in TreeView_DragDrop event:

' ...find it on Form
Dim destListView As ListView
destListView = CType(Me.Controls.Find(destListViewName, True)(0), ListView)

' Get entire ListViewItem from source List View
Dim sourceListViewItem As ListViewItem
sourceListViewItem = sourceListView.FindItemWithText(itemText)

' First remove it from source ListView
sourceListView.Items.Remove(sourceListViewItem)
' ...then add item to destinated ListView...
destListView.Items.Add(sourceListViewItem)
Member 10491813 17-Mar-14 7:00am    
Thanks again Marcin
101%
I appreciate Your help
God bless you
Marcin Kozub 17-Mar-14 7:01am    
Glad I could help you :)
HI,

It is very easy, you can do this using jquery

JavaScript
$(".DraggableElementClass").draggable(_
$(".DropableElementClass").droppable({
       drop: function (event, ui) {
            // droped elemet
        $(ui.draggable).closest('UL').append($(this).closest('li'));
        }
    });


Hope it will help.
 
Share this answer
 
Comments
Marcin Kozub 17-Mar-14 2:59am    
Did you read the question??? Where he/she mention about JavaScript?
Downvote...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900