Hide

SftTree/NET 2.0 - Tree Control for Windows Forms

Display
Print

DragDropSample1 (VB)

This sample demonstrates drag & drop within the tree control, dragging data to a textbox and dropping data on the tree control from File Explorer.

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

Public Class Form1

Private m_PlusImage As Image = Bitmap.FromFile("..\\..\\ExpandableNormal.bmp") ' a small + bitmap
Private m_MinusImage As Image = Bitmap.FromFile("..\\..\\CollapsableNormal.bmp") ' a small - bitmap

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ' This sample demonstrates drag & drop using one textbox control.
    ' To prepare for this sample, create a new project with a blank form and add
    ' a SftTree/NET control named sftTree1.
    ' In addition, a textbox named textbox1 is needed. Try dragging data from the
    ' tree control to the text box.
    ' It is also possible to drag files from Windows Explorer to the tree control.
    ' No files are moved/copied, merely their names are added to the tree control.
    sftTree1.Initializing = True
    sftTree1.Columns.Count = 1
    For i As Integer = 0 To 9
        Dim item As ItemClass = sftTree1.ItemCollection.Add("Item " & i.ToString())
        For ic As Integer = 0 To 9
            Dim child As ItemClass
            child = item.Add("Child item " & ic.ToString())
        Next
    Next
    sftTree1.ItemCollection.Collapse(CollapseStyleEnum.All)
    sftTree1.Columns.MakeOptimal(0, False)
    sftTree1.RecalcHorizontalExtent()

    ' We need to set up the tree control as a drop target
    sftTree1.AllowDrop = True
    sftTree1.AutoExpandDragDrop = True
    sftTree1.AutoExpandArea = AutoExpandAreaEnum.AllColumns
    sftTree1.Initializing = False

    ' We also need to set up the text box so we can drop data
    textBox1.AllowDrop = True
End Sub

Private Sub sftTree1_DragDetected(ByVal sender As Object, ByVal e As Softelvdm.SftTreeNET.DragDetectedEventArgs) Handles sftTree1.DragDetected
    Debug.Print("We're starting to drag something")
    DumpValues(e)

    ' based on what we are dragging, make up a string
    Dim s As String = ""
    s = e.Area.ToString() & " in "
    s = s & e.Item.UsageLocation.ToString() & ": "
    If Not e.Cell Is Nothing Then
        If e.Cell.Text = "" Then
            s = s & "(empty cell) "
        Else
            s = s & e.Cell.Text & " "
        End If
    ElseIf Not e.RowHeader Is Nothing Then
        If e.RowHeader.Text = "" Then
            s = s & "(empty row header) "
        Else
            s = s & e.RowHeader.Text & " "
        End If
    ElseIf Not e.Item Is Nothing Then
        s = s & " an item "
    End If
    e.Handled = True
    sftTree1.DoDragDrop(s, DragDropEffects.Copy)
End Sub

Private Sub textBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles textBox1.DragEnter
    Debug.Write("*** textBox1 DragEnter ")
    DumpValues(e)
    If e.Data.GetDataPresent(DataFormats.UnicodeText) Then
        e.Effect = DragDropEffects.Copy
    End If
End Sub
Private Sub textBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles textBox1.DragDrop
    Debug.Write("*** textBox1 DragDrop ")
    DumpValues(e)
    If e.Data.GetDataPresent(DataFormats.UnicodeText) Then
        textBox1.Text = e.Data.GetData("System.String")
    End If
End Sub

Private Sub sftTree1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles sftTree1.DragEnter
    Debug.Write("*** sftTree1 DragEnter ")
    DumpValues(e)
    If e.Data.GetDataPresent(DataFormats.UnicodeText) Then
        e.Effect = DragDropEffects.Copy
    ElseIf e.Data.GetDataPresent(DataFormats.FileDrop) Then
        e.Effect = DragDropEffects.Copy
    Else
        e.Effect = DragDropEffects.None
    End If
End Sub

Private Sub sftTree1_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles sftTree1.DragOver
    ' in this example, if we don't have a valid drop target,
    ' we STILL allow drops
    If sftTree1.DropTarget Is Nothing Then
        e.Effect = DragDropEffects.Copy
    End If
End Sub

Private Sub sftTree1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles sftTree1.DragDrop
    Debug.Write("*** sftTree1 DragDrop ")
    DumpValues(e)

    Dim item As ItemClass = sftTree1.DropTarget

    Dim s As String = ""
    Dim a As Array = Nothing
    If e.Data.GetDataPresent(DataFormats.UnicodeText) Then
        s = e.Data.GetData("System.String")
    ElseIf (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
        a = e.Data.GetData(DataFormats.FileDrop)
    Else
        Return
    End If

    Dim newItem As ItemClass = Nothing

    sftTree1.Initializing = True
    If s.Length > 0 Then
        If Not item Is Nothing Then
            newItem = item.Add(s)
            item.Expand(ExpandStyleEnum.DirectOnly)
        Else
            newItem = sftTree1.ItemCollection.Add(s)
        End If
    Else
        For Each o As Object In a
            s = o
            If Not item Is Nothing Then
                newItem = item.Add(s)
                item.Expand(ExpandStyleEnum.DirectOnly)
            Else
                newItem = sftTree1.ItemCollection.Add(s)
            End If
        Next
    End If
    sftTree1.Columns.MakeOptimal(0, False)
    sftTree1.RecalcHorizontalExtent()
    newItem.ScrollIntoView()
    sftTree1.FocusObject = newItem
    newItem.Selected = True
    sftTree1.Initializing = False
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 s As String
        On Error Resume Next
        pi.GetValue(o, New Object() )
        Debug.Write(" " & pi.Name & " " & s)
    Next
    Dim afi() As FieldInfo = o.GetType().GetFields()
    For Each fi As FieldInfo In afi
        Dim t As Object = fi.GetValue(o)
        Dim s As String = "(null)"
        If Not t Is Nothing Then s = t.ToString()
        Debug.Write(" " & fi.Name & " " & s)
    Next
    Debug.WriteLine("")
End Sub

End Class