Hide

SftTree/NET 2.0 - Tree Control for Windows Forms

Display
Print

DragDrop Sample (VB)

This sample demonstrates copying/moving items within the same and between two tree controls using drag & drop.

The source code is located at C:\Program Files (x86)\Softelvdm\SftTree NET 2.0\Samples\VB\DragDrop.

Imports Softelvdm.SftTreeNET
Imports Softelvdm.Controls
Imports System.Reflection

Public Class Form1

Private Sub CloseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseButton.Click
    Close()
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    SftTree1.Initializing = True
    SftTree1.AllowDrop = True
    SftTree1.AutoExpandDragDrop = True
    SftTree1.Columns.Count = 2
    SftTree1.Headers(0, 0).Text = "Left Tree Control"
    For c As Integer = 1 To SftTree1.Columns.Count - 1
        SftTree1.Headers(0, c).Text = "Column " + c.ToString()
    Next
    For i As Integer = 0 To 100 - 1
        Dim itemParent As ItemClass = SftTree1.ItemCollection.Add(New String() {"Item " + i.ToString(), "Cell 1 in Item " + i.ToString(), "Cell 2 in Item " + i.ToString()})
        itemParent.RowHeader.Text = i.ToString()
        i = i + 1
        Dim item As ItemClass = itemParent.Add(New String() {"Item " + i.ToString(), "Cell 1 in Item " + i.ToString(), "Cell 2 in Item " + i.ToString()})
        item.RowHeader.Text = i.ToString()
        i = i + 1
        Dim subitem As ItemClass = item.Add(New String() {"Item " + i.ToString(), "Cell 1 in Item " + i.ToString(), "Cell 2 in Item " + i.ToString()})
        subitem.RowHeader.Text = i.ToString()
        i = i + 1
        Dim child As ItemClass = subitem.Add(New String() {"Item " + i.ToString(), "Cell 1 in Item " + i.ToString(), "Cell 2 in Item " + i.ToString()})
        child.RowHeader.Text = i.ToString()
        i = i + 1
        child = item.Add(New String() {"Item " + i.ToString(), "Cell 1 in Item " + i.ToString(), "Cell 2 in Item " + i.ToString()})
        child.RowHeader.Text = i.ToString()
    Next
    SftTree1.Columns.MakeOptimal(0, False)
    SftTree1.RowHeaders.MakeOptimal(0, False)
    SftTree1.RecalcHorizontalExtent()
    SftTree1.Initializing = False

    SftTree2.Initializing = True
    SftTree2.AllowDrop = True
    SftTree2.AutoExpandDragDrop = True
    SftTree2.Columns.Count = 3
    SftTree2.RowHeaders.Width = 0
    SftTree2.Headers(0, 0).Text = "Right Tree Control"
    For c As Integer = 1 To SftTree2.Columns.Count - 1
        SftTree2.Headers(0, c).Text = "Column " + c.ToString()
    Next
    For i As Integer = 0 To 100 - 1
        Dim itemParent As ItemClass = SftTree2.ItemCollection.Add(New String() {"Item " + i.ToString(), "Cell 1 in Item " + i.ToString(), "Cell 2 in Item " + i.ToString()})
        itemParent.RowHeader.Text = i.ToString()
        i = i + 1
        Dim item As ItemClass = itemParent.Add(New String() {"Item " + i.ToString(), "Cell 1 in Item " + i.ToString(), "Cell 2 in Item " + i.ToString()})
        item.RowHeader.Text = i.ToString()
        i = i + 1
        Dim subitem As ItemClass = item.Add(New String() {"Item " + i.ToString(), "Cell 1 in Item " + i.ToString(), "Cell 2 in Item " + i.ToString()})
        subitem.RowHeader.Text = i.ToString()
        i = i + 1
        Dim child As ItemClass = subitem.Add(New String() {"Item " + i.ToString(), "Cell 1 in Item " + i.ToString(), "Cell 2 in Item " + i.ToString()})
        child.RowHeader.Text = i.ToString()
        i = i + 1
        child = item.Add(New String() {"Item " + i.ToString(), "Cell 1 in Item " + i.ToString(), "Cell 2 in Item " + i.ToString()})
        child.RowHeader.Text = i.ToString()
    Next
    SftTree2.Columns.MakeOptimal(0, False)
    SftTree2.RecalcHorizontalExtent()
    SftTree2.Initializing = False
End Sub

Private Sub sftTree1_DragDetected(ByVal sender As System.Object, ByVal e As Softelvdm.SftTreeNET.DragDetectedEventArgs) Handles SftTree1.DragDetected
    Debug.Write("sftTree1_DragDetected ")
    DumpValues(e)
    ' get the current item
    If e.Item Is Nothing Then Return
    Dim item As ItemClass = e.Item
    ' don't drag headers, footers
    If item.UsageLocation <> UsageLocationEnum.items Then Return
    ' start dragging the item
    SftTree1.DoDragDrop(e.Item, DragDropEffects.Copy Or DragDropEffects.Move)
End Sub

Private Sub sftTree1_DragOver(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles SftTree1.DragOver
    Debug.WriteLine("sftTree1_DragOver ")
    e.Effect = DragDropEffects.None
    If Not SftTree1.DropTarget Is Nothing And e.Data.GetDataPresent(GetType(ItemClass)) Then
        ' Copy or move based on control key
        If ((Control.ModifierKeys And Keys.Control) <> 0) Then
            e.Effect = DragDropEffects.Copy
        Else
            e.Effect = DragDropEffects.Move
        End If
    End If
End Sub

Private Sub sftTree1_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles SftTree1.DragDrop
    Debug.Write("sftTree1_DragDrop ")
    DumpValues(e)
    If Not SftTree1.DropTarget Is Nothing And e.Data.GetDataPresent(GetType(ItemClass)) Then
        Dim originalItem As ItemClass = e.Data.GetData(GetType(ItemClass))
        Dim targetItem As ItemClass = SftTree1.DropTarget
        Dim newItem As ItemClass = Nothing
        If e.Effect = DragDropEffects.Move Then
            Try
                newItem = originalItem.Move(targetItem, MoveStyleEnum.TargetAsParentInsertFirst)
            Catch exc As Exception
                MessageBox.Show(exc.Message)
            End Try
        Else
            Try
                newItem = originalItem.Copy(targetItem, CopyStyleEnum.TargetAsParentInsertFirst, True)
            Catch exc As Exception
                MessageBox.Show(exc.Message)
            End Try
        End If
        If Not newItem Is Nothing Then
            SftTree1.Columns.MakeOptimal(0, False)
            newItem.ScrollIntoView()
        End If
    End If
End Sub

Private Sub sftTree2_DragDetected(ByVal sender As System.Object, ByVal e As Softelvdm.SftTreeNET.DragDetectedEventArgs) Handles SftTree2.DragDetected
    Debug.Write("sftTree2_DragDetected ")
    DumpValues(e)
    ' get the current item
    If e.Item Is Nothing Then Return
    Dim item As ItemClass = e.Item
    ' don't drag headers, footers
    If item.UsageLocation <> UsageLocationEnum.items Then Return
    ' start dragging the item
    SftTree2.DoDragDrop(e.Item, DragDropEffects.Copy Or DragDropEffects.Move)
End Sub

Private Sub sftTree2_DragOver(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles SftTree2.DragOver
    Debug.WriteLine("sftTree2_DragOver ")
    e.Effect = DragDropEffects.None
    If Not SftTree2.DropTarget Is Nothing And e.Data.GetDataPresent(GetType(ItemClass)) Then
        ' Copy or move based on control key
        If ((Control.ModifierKeys And Keys.Control) <> 0) Then
            e.Effect = DragDropEffects.Copy
        Else
            e.Effect = DragDropEffects.Move
        End If
    End If
End Sub

Private Sub sftTree2_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles SftTree2.DragDrop
    Debug.Write("sftTree2_DragDrop ")
    DumpValues(e)
    If Not SftTree2.DropTarget Is Nothing And e.Data.GetDataPresent(GetType(ItemClass)) Then
        Dim originalItem As ItemClass = e.Data.GetData(GetType(ItemClass))
        Dim targetItem As ItemClass = SftTree2.DropTarget
        Dim newItem As ItemClass = Nothing
        If e.Effect = DragDropEffects.Move Then
            Try
                newItem = originalItem.Move(targetItem, MoveStyleEnum.TargetAsParentInsertFirst)
            Catch exc As Exception
                MessageBox.Show(exc.Message)
            End Try
        Else
            Try
                newItem = originalItem.Copy(targetItem, CopyStyleEnum.TargetAsParentInsertFirst, True)
            Catch exc As Exception
                MessageBox.Show(exc.Message)
            End Try
        End If
        If Not newItem Is Nothing Then
            SftTree2.Columns.MakeOptimal(0, False)
            newItem.ScrollIntoView()
        End If
    End If
End Sub

' This is a small helper routine to show all properties and fields of an object
Private Sub DumpValues(ByVal o As Object)
    Dim api() As PropertyInfo = o.GetType().GetProperties()
    For Each pi As PropertyInfo In api
        Dim ov = pi.GetValue(o, New Object() )
        Dim s As String = " " & pi.Name
        If Not ov Is Nothing Then s = s + " " + ov.ToString()
        Debug.Write(s)
    Next
    Dim afi() As FieldInfo = o.GetType().GetFields()
    For Each fi As FieldInfo In afi
        Dim ov = fi.GetValue(o)
        Dim s As String = " " & fi.Name
        If Not ov Is Nothing Then s = s + " " + ov.ToString()
        Debug.Write(s)
    Next
    Debug.WriteLine("")
End Sub

End Class