Search KB Filter article types
TreeView CodeSample - Dragging a TreeNode and dropping on a Button
Categories: Trees, Drag & Drop
Tags: Developers, Drag & Drop, 1. Beginner, Pre v6.3, v6.3, v6.4 and Later, 2. Intermediate, 3. Advanced
Revision: 1
Posted: 27/Sep/2009
Updated: 23/July/2010
Status: Publish
Types: Article

This article will have a few sections added to it soon, based on the following article type skeleton: CodeSample
Overview

This samplecode demonstrates the basic idea how to use DragDrop event on a TreeView to drag a TreeNode and drop on a Button.

Note the use of Casting inside the DragDrop event handler, where you cast the DragEventArgs type to DragDropEventArgs, which gives you access to the vital Source, SourceMember, Target and TargetMember properties of the drop.

VB.NET Code
Imports Gizmox.WebGUI.Forms
Public Class WithImage
 
    Sub New()
 
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
 
        ' Add any initialization after the InitializeComponent() call.
        DefineControls()
    End Sub
 
    Friend WithEvents TreeView1 As Gizmox.WebGUI.Forms.TreeView
    Friend WithEvents btnTarget As Gizmox.WebGUI.Forms.Button
 
    Private Sub DefineControls()
        Me.TreeView1 = New Gizmox.WebGUI.Forms.TreeView
        Me.btnTarget = New Gizmox.WebGUI.Forms.Button
 
        '
        'TreeView1
        '
        Me.TreeView1.DragTargets = New Gizmox.WebGUI.Forms.Component() {CType(Me.btnTarget, Gizmox.WebGUI.Forms.Component)}
        Me.TreeView1.Font = New System.Drawing.Font("Tahoma", 12.0!, System.Drawing.FontStyle.Bold)
        Me.TreeView1.Location = New System.Drawing.Point(12, 12)
        Me.TreeView1.Name = "TreeView1"
        Me.TreeView1.Size = New System.Drawing.Size(242, 215)
        Me.TreeView1.TabIndex = 0
        Me.TreeView1.Nodes.Add("Node 1")
        Me.TreeView1.Nodes.Add("Node 2")
        Me.TreeView1.Nodes.Add("Node 3")
        Me.TreeView1.Nodes.Add("Node 4")
        Me.TreeView1.Nodes.Add("Node 5")
 
        '
        'btnTarget
        '
        Me.btnTarget.AllowDrop = True
        Me.btnTarget.Location = New System.Drawing.Point(261, 13)
        Me.btnTarget.Name = "btnTarget"
        Me.btnTarget.Size = New System.Drawing.Size(75, 23)
        Me.btnTarget.TabIndex = 0
        Me.btnTarget.Text = "Drop Target"
 
        Me.Controls.Add(Me.btnTarget)
        Me.Controls.Add(Me.TreeView1)
 
    End Sub
    Private Sub Button1_DragDrop(ByVal sender As System.Object, ByVal e As Gizmox.WebGUI.Forms.DragEventArgs) Handles btnTarget.DragDrop
        If e.GetType Is GetType(DragDropEventArgs) Then
            Dim dd As DragDropEventArgs = e
            If dd.Source IsNot Nothing _
                AndAlso dd.SourceMember IsNot Nothing _
                AndAlso dd.Source.GetType() Is GetType(TreeView) _
                AndAlso dd.SourceMember.GetType() Is GetType(TreeNode) Then
                Dim tn As TreeNode = dd.SourceMember
                MessageBox.Show("You dropped: " + tn.Text)
            End If
        End If
    End Sub
 
End Class

C# Code
The conversion of this code to C# has not been completed yet.

About the author

Related Articles

Trees  
Title Update Author
Control of a combobox with a checked list and a tree.
Tags: Developers, Navigation, 2. Intermediate, 3. Advanced, Customization, Layouting, Navigation, Pre v6.3, v6.3, v6.4 and Later
06/Jan/2008    2008/01/06
This video demonstrates how to populate a TreeView control.
Tags: Developers, Data Binding, 1. Beginner, Data Binding, Pre v6.3, v6.3, v6.4 and Later, 2. Intermediate, 3. Advanced
01/Jan/2009    2009/01/01
This code snippet posted By NEC_VERSA demonstrates how to find a node in a tree view.
Tags: Developers, 1. Beginner, Pre v6.3, v6.3, v6.4 and Later, 2. Intermediate, 3. Advanced
12/Jan/2008    2008/01/12
This article contains code that demonstrates how to create a personalized look for TreeNodes that have the mouse hovering over them while dragging another element.
Tags: Developers, Drag & Drop, Events, C#, 2. Intermediate, 3. Advanced, Customization, v6.3, v6.4 and Later
06/Jan/2010    2010/01/06
Tags: Architects, Developers, 1. Beginner, 2. Intermediate, 3. Advanced, Pre v6.3, v6.3, v6.4 and Later
04/Dec/2010    2010/12/04
Tags: Developers, 1. Beginner, Pre v6.3, v6.3, v6.4 and Later, 2. Intermediate, 3. Advanced
23/July/2010    2010/07/23
.NET Web, Cloud and Mobile application delivery platform | Sitemap | Terms of Use | Privacy Statement | Copyright © 2005-2011 Visual WebGui®       Visual WebGui weblog on ASP.NET Visual WebGui Group on LinkedIn Visual WebGui updates on Twitter Visual WebGui Page on Facebook Visual WebGui YouTube Channel Visual WebGui Platform News RSS