Hide

SftTree/OCX 7.5 - ActiveX Tree Control

Display
Print

DragDrop Sample (VB.NET)

This sample illustrates drag & drop.

The source code is located at C:\Program Files (x86)\Softelvdm\SftTree OCX 7.5\Samples\Visual Studio - VB.NET\DragDrop\Form1.vb or C:\Program Files\Softelvdm\SftTree OCX 7.5\Samples\Visual Studio - VB.NET\DragDrop\Form1.vb (on 32-bit Windows versions).


    Private Sub Command1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Command1.Click
        Application.Exit()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DropTargetPic.AllowDrop = True
        Dim i As Integer
        AxSftTree1.Items.Add("Item 0")
        i = AxSftTree1.Items.Add("Item 1")
        AxSftTree1.get_Item(i).Level = 1
        i = AxSftTree1.Items.Add("Item 2")
        AxSftTree1.get_Item(i).Level = 2
        i = AxSftTree1.Items.Add("Item 3")
        AxSftTree1.get_Item(i).Level = 1
    End Sub

    Private Sub AxSftTree1_DragStarting(ByVal sender As Object, ByVal e As AxSftTreeLib75._DSftTreeEvents_DragStartingEvent) Handles AxSftTree1.DragStarting
        AxSftTree1.CancelMode()    ' .NET controls drag&drop, so tree control needs to cancel

        ' This section demonstrates drag (&drop) using the .NET DoDragDrop method
        ' THIS CODE IS ONLY USED IF DRAGMETHOD IS SET TO  ( 1 - Manual ) or SftTreeDragMethodConstants.dragSftTreeManual

        Dim curr As Integer
        curr = AxSftTree1.Items.Current

        ' cell text
        Dim Data As System.Windows.Forms.DataObject
        Data = New System.Windows.Forms.DataObject()
        Data.SetData(DataFormats.Text, AxSftTree1.get_Cell(curr, 0).Text)

        ' item picture
        Dim img As SftPictureObject
        If AxSftTree1.get_Item(curr).Image.Type = SftPictureTypeConstants.sftTypeIDispatch Then
            img = AxSftTree1.get_Item(curr).Image
        Else
            If AxSftTree1.get_Item(curr).Expanded Then
                img = AxSftTree1.Items.ItemImageExpanded
            ElseIf AxSftTree1.get_Item(curr).DependentAllCount > 0 Then
                img = AxSftTree1.Items.ItemImageExpandable
            Else
                img = AxSftTree1.Items.ItemImageLeaf
            End If
        End If
        Dim sendingImage As Image = OLECvt.ToImage(img.Picture)
        Data.SetData(DataFormats.Bitmap, True, sendingImage)
        DoDragDrop(Data, DragDropEffects.Copy)
    End Sub

    Private Sub AxSftTree1_OLEStartDrag(ByVal sender As Object, ByVal e As AxSftTreeLib75._DSftTreeEvents_OLEStartDragEvent) Handles AxSftTree1.OLEStartDrag

        ' This section demonstrates drag&drop) using OLE mechanisms built into this control
        ' THIS CODE IS ONLY USED IF DRAGMETHOD IS SET TO  ( 3 - OLEDrag ) or SftTreeDragMethodConstants.dragSftTreeOLE

        Dim curr As Integer
        curr = AxSftTree1.Items.Current

        ' cell text
        e.data.SetData(AxSftTree1.get_Cell(curr, 0).Text, SftOLEClipboardConstants.sftCFText)

        ' item picture
        Dim img As SftPictureObject
        If AxSftTree1.get_Item(curr).Image.Type = SftPictureTypeConstants.sftTypeIDispatch Then
            img = AxSftTree1.get_Item(curr).Image
        Else
            If AxSftTree1.get_Item(curr).Expanded Then
                img = AxSftTree1.Items.ItemImageExpanded
            ElseIf AxSftTree1.get_Item(curr).DependentAllCount > 0 Then
                img = AxSftTree1.Items.ItemImageExpandable
            Else
                img = AxSftTree1.Items.ItemImageLeaf
            End If
        End If
        e.data.SetData(img.Picture, SftOLEClipboardConstants.sftCFDIB)
        e.data.SetData(img.Picture, SftOLEClipboardConstants.sftCFBitmap)
        e.allowedEffects = 3
    End Sub

    Private Sub DropTarget_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DropTarget.DragEnter
        If e.Data.GetDataPresent(DataFormats.Text, True) Or e.Data.GetDataPresent(DataFormats.Bitmap, True) Then
            e.Effect = DragDropEffects.Copy
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub

    Private Sub DropTarget_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DropTarget.DragDrop
        Dim arrayOfFormats As [String]() = e.Data.GetFormats(True) ' informational

        If e.Data.GetDataPresent(DataFormats.Text, True) Then
            DropTarget.Text = e.Data.GetData(DataFormats.Text, True)
        End If
        If e.Data.GetDataPresent(DataFormats.Bitmap, True) Then
            DropTargetPic.Image = e.Data.GetData(DataFormats.Bitmap, True)
        End If
    End Sub

    Private Sub DropTargetPic_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DropTargetPic.DragEnter
        If e.Data.GetDataPresent(DataFormats.Bitmap, True) Then
            e.Effect = DragDropEffects.Copy
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub

    Private Sub DropTargetPic_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DropTargetPic.DragDrop
        Dim arrayOfFormats As [String]() = e.Data.GetFormats(True) ' informational
        If e.Data.GetDataPresent(DataFormats.Bitmap, True) Then
            Dim O As Object = e.Data.GetData(DataFormats.Bitmap, True)
            DropTargetPic.Image = O
        End If
    End Sub

    Private Sub AxSftTree1_OLEDragDrop(ByVal sender As Object, ByVal e As AxSftTreeLib75._DSftTreeEvents_OLEDragDropEvent) Handles AxSftTree1.OLEDragDrop
        ' get horizontal extent and width of column 0
        Dim horzExtent As Integer
        horzExtent = AxSftTree1.Items.HorizontalExtentPix
        Dim firstColumnWidth As Integer
        firstColumnWidth = AxSftTree1.get_Column(0).WidthPix

        Dim insertAt As Integer
        insertAt = AxSftTree1.Items.DropHighlight
        If insertAt < 0 Then Exit Sub
        If e.data.GetFormat(SftOLEClipboardConstants.sftCFText) Then
            Dim lvl As Integer
            lvl = AxSftTree1.get_Item(insertAt).Level
            Dim str As String
            str = e.data.GetData(SftOLEClipboardConstants.sftCFText)
            Dim newItem As Integer
            newItem = AxSftTree1.Items.Insert(insertAt + 1, str)
            AxSftTree1.get_Item(newItem).Level = lvl + 1
        End If
        If e.data.GetFormat(SftOLEClipboardConstants.sftCFDIB) Then
            AxSftTree1.get_Cell(insertAt, 0).Image.Picture = e.data.GetData(SftOLEClipboardConstants.sftCFDIB)
            AxSftTree1.get_Cell(insertAt, 0).ImageHAlign = SftTreeHAlignConstants.halignSftTreeRight
        End If
        If e.data.GetFormat(SftOLEClipboardConstants.sftCFBitmap) Then
            AxSftTree1.get_Cell(insertAt, 0).Image.Picture = e.data.GetData(SftOLEClipboardConstants.sftCFBitmap)
            AxSftTree1.get_Cell(insertAt, 0).ImageHAlign = SftTreeHAlignConstants.halignSftTreeRight
        End If
        If e.data.GetFormat(SftOLEClipboardConstants.sftCFFiles) Then
            AxSftTree1.BulkUpdate = True
            Dim lvl As Integer
            lvl = AxSftTree1.get_Item(insertAt).Level
            Dim newItem As Integer
            newItem = insertAt + 1
            Dim i As Integer
            For i = e.data.Files.Count To 1 Step -1
                newItem = AxSftTree1.Items.Insert(newItem, e.data.Files(i))
                AxSftTree1.get_Item(newItem).Level = lvl + 1
            Next
            AxSftTree1.BulkUpdate = False
        End If

        ' make horizontal extent and width of column 0 wider than previous
        ' setting, but never smaller
        AxSftTree1.ColumnsObj.MakeOptimal()
        AxSftTree1.Items.RecalcHorizontalExtent()

        If horzExtent > AxSftTree1.Items.HorizontalExtent Then
            AxSftTree1.Items.HorizontalExtent = horzExtent
        End If
        If firstColumnWidth > AxSftTree1.get_Column(0).WidthPix Then
            AxSftTree1.get_Column(0).WidthPix = firstColumnWidth
        End If
    End Sub

    Private Sub Picture1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Picture1.MouseDown
        Dim Data As System.Windows.Forms.DataObject
        Data = New System.Windows.Forms.DataObject()
        Data.SetData(DataFormats.Bitmap, True, Picture1.Image)
        DoDragDrop(Data, DragDropEffects.Copy)
    End Sub

    Private Sub Picture2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Picture2.MouseDown
        Dim Data As System.Windows.Forms.DataObject
        Data = New System.Windows.Forms.DataObject()
        Data.SetData(DataFormats.Dib, True, Picture2.Image)
        DoDragDrop(Data, DragDropEffects.Copy)
    End Sub

    Private Sub Text1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Text1.MouseDown
        Dim Data As System.Windows.Forms.DataObject
        Data = New System.Windows.Forms.DataObject()
        Data.SetData(DataFormats.Text, Text1.Text)
        DoDragDrop(Data, DragDropEffects.Copy)
    End Sub
End Class

Last Updated 08/13/2020 - (email)
© 2024 Softel vdm, Inc.