Article
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    APIs
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> C# >> Drag-Drop in TreeView in C#
Drag-Drop in TreeView in C#
Drag-Drop in TreeView in C#


by AVADHESH PATEL on 7/20/2012 4:03:19 PM

Views: 1438       Comments: 1

Drag-Drop in TreeView in C#

This article will briefly describe the basics of drag and drop in TreeView Control or Draggable TreeView Nodes in C#, the aim of the article is to describe how node dragging and swapping in TreeView, implemented in C#.

Steps: - Take a windows form and drag drop a TreeView Control and write code which is given below

Drag-Drop in TreeView in C#

Code:-

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace TreeViewDragDrop

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

            TreeNode rootNode = treeView1.Nodes.Add("USA");

 

            // Create the Child nodes for the root

            TreeNode states1 = rootNode.Nodes.Add("New York");

            TreeNode states2 = rootNode.Nodes.Add("Michigan");

            TreeNode states3 = rootNode.Nodes.Add("Wisconsin");

            TreeNode states4 = rootNode.Nodes.Add("California");

 

            // Create siblings nodes for the Child nodes

            TreeNode child = states1.Nodes.Add("Rochester");

            child = states1.Nodes.Add("New York");

            child = states1.Nodes.Add("Albany");

            child = states2.Nodes.Add("Detroit");

            child = states2.Nodes.Add("Ann Arbor");

            child = states2.Nodes.Add("Lansing");

            child = states3.Nodes.Add("Milwaukee");

            child = states3.Nodes.Add("Madison");

            child = states3.Nodes.Add("La Cross");

            child = states4.Nodes.Add("Los Angeles");

            child = states4.Nodes.Add("San Fransisco");

            child = states4.Nodes.Add("San Diego");

 

            // Expand all tree nodes when first loaded

            treeView1.AllowDrop = true;

            treeView1.ExpandAll();

 

 

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

        }

 

        private void treeView1_DragEnter(object sender, DragEventArgs e)

        {

            e.Effect = DragDropEffects.Move;

        }

 

        private void treeView1_DragDrop(object sender, DragEventArgs e)

        {

            TreeNode SourceNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");

            Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));

            TreeNode DestinationNode = ((TreeView)sender).GetNodeAt(pt);

           

            try

            {

                int sourceIndex = SourceNode.Index;

                int destIndex = DestinationNode.Index;

 

                if (SourceNode.Level == 1 && DestinationNode.Level == 1)

                {

                    if (sourceIndex < destIndex)

                    {

                        TreeNode parentNode = SourceNode.Parent;

                        SourceNode.Remove();

                        DestinationNode.Remove();

                        parentNode.Nodes.Insert(sourceIndex, DestinationNode);

                        parentNode.Nodes.Insert(destIndex, SourceNode);

                    }

                    else

                    {

                        TreeNode parentNode = SourceNode.Parent;

                        SourceNode.Remove();

                        DestinationNode.Remove();

                        parentNode.Nodes.Insert(sourceIndex - 1, DestinationNode);

                        parentNode.Nodes.Insert(destIndex, SourceNode);

                    }

                }

 

                else if (SourceNode.Level == 2 && DestinationNode.Level == 2)

                {

                   

                    if (SourceNode.Parent == DestinationNode.Parent)

                    {

                        if (sourceIndex < destIndex)

                        {

                            TreeNode parentNode = SourceNode.Parent;

                            SourceNode.Remove();

                            DestinationNode.Remove();

                            parentNode.Nodes.Insert(sourceIndex, DestinationNode);

                            parentNode.Nodes.Insert(destIndex, SourceNode);

                        }

                        else

                        {

                            TreeNode parentNode = SourceNode.Parent;

                            SourceNode.Remove();

                            DestinationNode.Remove();

                            parentNode.Nodes.Insert(sourceIndex - 1, DestinationNode);

                            parentNode.Nodes.Insert(destIndex, SourceNode);

                        }

                    }

                    else

                    {

                        TreeNode addNode = DestinationNode.Parent;

                        SourceNode.Remove();

                        addNode.Nodes.Insert(destIndex, SourceNode);

                       

                    }

                }

 

                else if (SourceNode.Level == 2 && DestinationNode.Level == 1)

                { 

                    if (DestinationNode.Nodes.Count==0)

                    {

                        SourceNode.Remove();

                        DestinationNode.Nodes.Insert(0, SourceNode);               

                    }

                    else

                    {

                        int i = DestinationNode.LastNode.Index;

                        SourceNode.Remove();

                        DestinationNode.Nodes.Insert(i + 1, SourceNode);

                    }

                }

            }

            catch

            {

                MessageBox.Show("Drag on node...");

            }

        }

 

        private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)

        {

            DoDragDrop(e.Item, DragDropEffects.Move);

        }

 

 

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)

        {

 

        }

    }

}

 

 

Output:-

Drag-Drop in TreeView in C#

Report Abuse Form
Reason:    
 


Print treeview data
by Manmohan Jha 2/19/2013 1:41:43 AM
How to print treeview data...

regard
Manmohan Jha
Report Abuse
Title :
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
     
 
Latest Article by AVADHESH PATELRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 5088
Advertisement
MindStick SurveyManager
Advertise with Us
  
Copyright © 2009 - 2013MindStick. All Rights Reserved.