Home All Groups Group Topic Archive Search About
Author
21 May 2009 11:33 AM
Co
Hi All,

I have aTreeview and a Listview on a form.
From the Listview I can drag and drop items onto another node of the
Treeview.
When I drop the item it is removed from the Listview.
But when I change my mind and don't drop the item is removed from the
Listview anyway.
How can I prevent this:

Private Sub ListView_ItemDrag(ByVal sender As Object, ByVal e As
System.Windows.Forms.ItemDragEventArgs) Handles ListView.ItemDrag
        Dim myItem As ListViewItem
        'Create an array of strings.
        Dim myItems(ListView.SelectedItems.Count - 1) As String
        Dim i As Integer = 0
        'Loop though the SelectedItems collection of the ListView
control.
        For Each myItem In ListView.SelectedItems
            'Add the Text of the ListViewItem to the array.
            myItems(i) = myItem.Text
            i = i + 1
        Next
        'DoDragDrop begins the drag-and-drop operation.
        'The data to be dragged is the array of strings.
        ListView.DoDragDrop(myItems, DragDropEffects.Move)
        Dim j As ListViewItem
        For Each j In ListView.SelectedItems
            'Remove the ListViewItem from the ListView control.
            ListView.Items.Remove(j)
        Next
    End Sub

Regards
Marco
The Netherlands

Author
21 May 2009 4:16 PM
Lloyd Sheen
Show quote Hide quote
"Co" <vonclausow***@gmail.com> wrote in message
news:2e551ee2-85ab-4681-8740-c8129b7c9991@s21g2000vbb.googlegroups.com...
> Hi All,
>
> I have aTreeview and a Listview on a form.
> From the Listview I can drag and drop items onto another node of the
> Treeview.
> When I drop the item it is removed from the Listview.
> But when I change my mind and don't drop the item is removed from the
> Listview anyway.
> How can I prevent this:
>
> Private Sub ListView_ItemDrag(ByVal sender As Object, ByVal e As
> System.Windows.Forms.ItemDragEventArgs) Handles ListView.ItemDrag
>        Dim myItem As ListViewItem
>        'Create an array of strings.
>        Dim myItems(ListView.SelectedItems.Count - 1) As String
>        Dim i As Integer = 0
>        'Loop though the SelectedItems collection of the ListView
> control.
>        For Each myItem In ListView.SelectedItems
>            'Add the Text of the ListViewItem to the array.
>            myItems(i) = myItem.Text
>            i = i + 1
>        Next
>        'DoDragDrop begins the drag-and-drop operation.
>        'The data to be dragged is the array of strings.
>        ListView.DoDragDrop(myItems, DragDropEffects.Move)
>        Dim j As ListViewItem
>        For Each j In ListView.SelectedItems
>            'Remove the ListViewItem from the ListView control.
>            ListView.Items.Remove(j)
>        Next
>    End Sub
>
> Regards
> Marco
> The Netherlands

You have to do two things.

First the DragDrop event must set the return code with a:

e.Effect = DragDropEffects.Copy
e.Effect = DragDropEffects.Move
e.Effect = DragDropEffects.None

Then in the routine that starts the DragDrop (DoDragDrop) you can then check
the status code and if it is a None then don't remove the items from the
ListView.

Hope this helps
LS
Author
21 May 2009 7:22 PM
Co
Show quote Hide quote
On 21 mei, 18:16, "Lloyd Sheen" <a...@b.c> wrote:
> "Co" <vonclausow***@gmail.com> wrote in message
>
> news:2e551ee2-85ab-4681-8740-c8129b7c9991@s21g2000vbb.googlegroups.com...
>
>
>
> > Hi All,
>
> > I have aTreeview and a Listview on a form.
> > From the Listview I can drag and drop items onto another node of the
> > Treeview.
> > When I drop the item it is removed from the Listview.
> > But when I change my mind and don't drop the item is removed from the
> > Listview anyway.
> > How can I prevent this:
>
> > Private Sub ListView_ItemDrag(ByVal sender As Object, ByVal e As
> > System.Windows.Forms.ItemDragEventArgs) Handles ListView.ItemDrag
> >        Dim myItem As ListViewItem
> >        'Create an array of strings.
> >        Dim myItems(ListView.SelectedItems.Count - 1) As String
> >        Dim i As Integer = 0
> >        'Loop though the SelectedItems collection of the ListView
> > control.
> >        For Each myItem In ListView.SelectedItems
> >            'Add the Text of the ListViewItem to the array.
> >            myItems(i) = myItem.Text
> >            i = i + 1
> >        Next
> >        'DoDragDrop begins the drag-and-drop operation.
> >        'The data to be dragged is the array of strings.
> >        ListView.DoDragDrop(myItems, DragDropEffects.Move)
> >        Dim j As ListViewItem
> >        For Each j In ListView.SelectedItems
> >            'Remove the ListViewItem from the ListView control.
> >            ListView.Items.Remove(j)
> >        Next
> >    End Sub
>
> > Regards
> > Marco
> > The Netherlands
>
> You have to do two things.
>
> First the DragDrop event must set the return code with a:
>
> e.Effect = DragDropEffects.Copy
> e.Effect = DragDropEffects.Move
> e.Effect = DragDropEffects.None
>
> Then in the routine that starts the DragDrop (DoDragDrop) you can then check
> the status code and if it is a None then don't remove the items from the
> ListView.
>
> Hope this helps
> LS

Could you please show me how to adapt my code?

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

        If e.Effect = DragDropEffects.None Then

        End If
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then

            Dim MyFiles() As String
            Dim MyRecord(11) As String
            Dim i As Integer
            Dim j As Integer

            Dim nKenMerk As String = CreateNewKenmerk()  'create a new
reference number for the document
            ' Assign the files to an array.
            MyFiles = CType(e.Data.GetData(DataFormats.FileDrop),
String())
            ' Loop through the array and add the files to the list.
            Dim LastKey As String = ""
            For i = 0 To MyFiles.Length - 1
                'now add the file to the database
                j = GetFileExtension(My.Computer.FileSystem.GetFileInfo
(MyFiles(i)).Extension)
                'Just the extension e.g. ".exe", soon to be type e.g.
"Application"
                MyRecord(0) = System.IO.Path.GetFileName(MyFiles(i))
                MyRecord(1) = "Open"
                MyRecord(2) = CStr(iCurrentNode)
                MyRecord(3) = CStr(j)
                MyRecord(4) = (My.Computer.FileSystem.GetFileInfo
(MyFiles(i)).Length \ 1000) & " kB"
                MyRecord(5) = "Admin"
                MyRecord(6) = (My.Computer.FileSystem.GetFileInfo
(MyFiles(i)).CreationTime).ToString("dd-MM-yyyy")
                MyRecord(7) = (My.Computer.FileSystem.GetFileInfo
(MyFiles(i)).LastWriteTime).ToString("dd-MM-yyyy")
                MyRecord(8) = ""
                MyRecord(9) = My.Computer.FileSystem.GetFileInfo
(MyFiles(i)).DirectoryName & "\"
                MyRecord(10) =
System.IO.Path.GetFileNameWithoutExtension(MyFiles(i))
                MyRecord(11) = nKenMerk
                LastKey = AddRecords2Database(MyRecord).ToString
                Dim Text As String = MyRecord(0)
                Dim lvitem As ListViewItem = ListView.Items.Add
(LastKey, Text, j)
                lvitem.SubItems.Add(nKenMerk)
                lvitem.SubItems.Add
(((My.Computer.FileSystem.GetFileInfo(MyFiles(i)).Length) \ 1000) & "
kB") 'File Size e.g. "2,400KB"
                lvitem.SubItems.Add("Admin")  'auteur
                lvitem.SubItems.Add((My.Computer.FileSystem.GetFileInfo
(MyFiles(i)).CreationTime).ToString("dd-MM-yyyy"))
                lvitem.SubItems.Add((My.Computer.FileSystem.GetFileInfo
(MyFiles(i)).LastWriteTime).ToString("dd-MM-yyyy"))
                lvitem.SubItems.Add("Open")   'status
                lvitem.SubItems.Add("")       'soort
                lvitem.SubItems.Add("")       'In/Uit
                lvitem.Tag = LastKey
            Next
            If LastKey > "" Then
                ListView.SelectedItems.Clear()
                ListView.Items(LastKey).Selected = True
                DocumentDetailHandler(ListView, Nothing)
            End If
        End If
        ListView.AutoResizeColumns
(ColumnHeaderAutoResizeStyle.HeaderSize)

    End Sub

Private Sub ListView_DragEnter(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles ListView.DragEnter
        'Check for the DataFormat string array.
        If e.Data.GetDataPresent("System.String[]") Then
            'If the data stored is a string array,
            'set the Effect of drag-and-drop operation to Move.
            e.Effect = DragDropEffects.Move
        Else
            'Else set the Effect of drag-and-drop operation to None.
            e.Effect = DragDropEffects.None
        End If
    End Sub

    Private Sub ListView_DragOver(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles ListView.DragOver
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            e.Effect = DragDropEffects.Copy
        End If
    End Sub

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

        Dim myItem As ListViewItem
        'Create an array of strings.
        Dim myItems(ListView.SelectedItems.Count - 1) As String
        Dim i As Integer = 0
        'Loop though the SelectedItems collection of the ListView
control.
        For Each myItem In ListView.SelectedItems
            'Add the Text of the ListViewItem to the array.
            myItems(i) = myItem.Text
            i = i + 1
        Next
        'DoDragDrop begins the drag-and-drop operation.
        'The data to be dragged is the array of strings.
        ListView.DoDragDrop(myItems, DragDropEffects.Move)
        Dim j As ListViewItem
        For Each j In ListView.SelectedItems
            'Remove the ListViewItem from the ListView control.
            ListView.Items.Remove(j)
        Next
    End Sub

        Public Sub TreeView_DragDrop( _
        ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.DragEventArgs) _
        Handles TreeView.DragDrop

        ' Handle the Drag/Drop event.

        Dim pt As Point
        Dim newID As Integer
        Dim myItem As ListViewItem
        Dim myItems(ListView.SelectedItems.Count - 1) As String
        Dim i As Integer = 0
        'Loop though the SelectedItems collection of the ListView
control.
        For Each myItem In ListView.SelectedItems
            'Add the Text of the ListViewItem to the array.
            myItems(i) = myItem.Tag
            i = i + 1
        Next

        'Create a new node
        Dim DestinationNode

        ' Get the treeview point
        pt = TreeView.PointToClient(New Point(e.X, e.Y))

        ' Get a handle to the node the item was dragged onto
        DestinationNode = TreeView.GetNodeAt(pt)
        newID = DestinationNode.row("ID")
        FileChangeFolder(iCurrentNode, newID, myItems)

    End Sub


Marco