Click here to Skip to main content
15,895,799 members
Articles / Programming Languages / C#

Tree Nodes Navigator

Rate me:
Please Sign up or sign in to vote.
3.36/5 (7 votes)
19 Aug 2008CPOL3 min read 39.5K   889   19  
The class presented here allows the user to navigate the TreeNodes in a TreeView control
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace ABS.TreeNodeNavigator
{
    #region  DISCLAIMER
    /* THE CLASS IS PROVIDED "AS IS". ANY ECONOMIC (OR OTHERWISE) 
    /* LOSS/DAMAGE INCURRED DIRECTLY OR INDIRECTLY BECAUSE OF THIS 
    /* CLASS IS TO BE BEARED BY THE USER. THE AUTHOR IS IN NO WAY 
    /* RESPONSIBLE FOR ANY SUCH LOSS/DAMAGE.*/
    #endregion
    #region class 'Navigator'
    public sealed class Navigator
    {
        #region CLASS DESCRIPTION
        /* 
        Name of class     : ABS.TreeNodeNavigator.Navigator
        Purpose           : To navigate Treeview nodes across the tree hierarchy.
        Class Variables   : transactionStatus enum, db oracleDatabase
        Fucntions         : MoveUp()
                            MoveDown()
                            MoveRight()
                            MoveLeft()                     
        Dependencies      : System.Windows.Forms
        Author            : Siddhartha Sarkar
        Creation Date     : 1 August 2008
        Version           : Draft  
        Modification Hist :
        Modified By           Modified Date           Description(Modified/Added Function Name)
                                                                                                               
        */
        #endregion
        #region"User defined variable"
        private TreeNode trevViewNode = null;
        private TreeView trvParent = null;
        #endregion
        #region "Property"
        public TreeNode TreeViewNode
        {
            get
            {
                return trevViewNode;
            }
            set
            {
                trevViewNode = value;
                if (trevViewNode.TreeView == null)
                    throw new Exception("The tree node that has been assigned does not belong to a treeview.\n" +
                        "Please assign the node to a treeview then assign it to the property of the navigator class.");
                else
                    trvParent = trevViewNode.TreeView;
            }
        }
        #endregion
        #region "Function to Move a node of the treeview Up"
        /// <summary>
        /// Function to Move a node of the treevie to the left
        /// </summary>
        public void MoveUp()
        {
            try
            {
                if (trevViewNode == null)
                    throw new NullReferenceException("The Property 'TreeViewNode' hasn't been assigned. Please assing the treeview you want to operate on.");
                else
                {
                    if (trevViewNode.Index != 0 )
                    {
                        if (trevViewNode.Level > 0)
                        {
                            int index = trevViewNode.Index;
                            TreeNode parent = trevViewNode.Parent;
                            //assign
                            TreeNode[] tempNode = new TreeNode[] { (TreeNode)trevViewNode.PrevNode.Clone(), (TreeNode)trevViewNode.Clone() };
                            //remove
                            parent.Nodes.RemoveAt(trevViewNode.Index - 1);
                            parent.Nodes.RemoveAt(trevViewNode.Index);
                            //insert
                            parent.Nodes.Insert(index - 1, tempNode[1]);
                            parent.Nodes.Insert(index, tempNode[0]);
                            //select the node
                            trvParent.SelectedNode = tempNode[1];
                        }
                        else
                        {
                            int index = trevViewNode.Index;
                            //assign
                            TreeNode[] tempNode = new TreeNode[] { (TreeNode)trevViewNode.PrevNode.Clone(), (TreeNode)trevViewNode.Clone() };
                            //remove
                            trvParent.Nodes.RemoveAt(trevViewNode.Index - 1);
                            trvParent.Nodes.RemoveAt(trevViewNode.Index);
                            //insert
                            trvParent.Nodes.Insert(index - 1, tempNode[1]);
                            trvParent.Nodes.Insert(index, tempNode[0]);
                            //select the node
                            trvParent.SelectedNode = tempNode[1];
                        }
                    }
                }
            }
            catch (NullReferenceException ex)
            {
                throw new Exception(ex.Message);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion
        #region "Function to move a node of the treeview Right"
        public void MoveRight()
        {
            try
            {
                if (trevViewNode == null)
                    throw new NullReferenceException("The Property 'TreeViewNode' hasn't been assigned. Please assing the treeview you want to operate on.");
                else
                {
                    if (trevViewNode.Index != 0)
                    {
                        if (trevViewNode.Parent != null)
                        {
                            int index = trevViewNode.Index;
                            TreeNode parent = trevViewNode.Parent;
                            TreeNode tempNode = (TreeNode)trevViewNode.Clone();
                            parent.Nodes[index - 1].Nodes.Add(tempNode);
                            trevViewNode.Remove();
                            //select the node
                            trvParent.SelectedNode = tempNode;
                        }
                        else
                        {
                            int index = trevViewNode.Index;
                            TreeView parent = trevViewNode.TreeView;
                            TreeNode tempNode = (TreeNode)trevViewNode.Clone();
                            trvParent.Nodes[index - 1].Nodes.Add(tempNode);
                            trevViewNode.Remove();
                            //select the node
                            trvParent.SelectedNode = tempNode;
                        }
                    }
                }
            }
            catch (NullReferenceException ex)
            {
                throw new Exception(ex.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion
        #region "Function to move a node of the treeview Down"
        public void MoveDown()
        {
            try
            {
                if (trevViewNode == null)
                    throw new NullReferenceException("The Property 'TreeViewNode' hasn't been assigned. Please assing the treeview you want to operate on.");
                else
                {
                    if (trevViewNode.NextNode != null)
                    {
                        if (trevViewNode.Level >= 1)
                        {
                            int index = trevViewNode.Index;
                            TreeNode parent = trevViewNode.Parent;
                            //assign
                            TreeNode[] tempNode = new TreeNode[] { (TreeNode)trevViewNode.Clone(), (TreeNode)trevViewNode.NextNode.Clone() };
                            //remove                            
                            trevViewNode.NextNode.Remove();
                            trevViewNode.Remove();
                            //insert
                            parent.Nodes.Insert(index, tempNode[1]);
                            parent.Nodes.Insert(index + 1, tempNode[0]);
                            //select the node required
                            trvParent.SelectedNode = tempNode[0];
                        }
                        else
                        {
                            int index = trevViewNode.Index;
                            //assign
                            TreeNode[] tempNode = new TreeNode[] { (TreeNode)trevViewNode.Clone(), (TreeNode)trevViewNode.NextNode.Clone() };
                            //remove                            
                            trevViewNode.NextNode.Remove();
                            trevViewNode.Remove();
                            //insert
                            trvParent.Nodes.Insert(index, tempNode[1]);
                            trvParent.Nodes.Insert(index + 1, tempNode[0]);
                            //select the node required
                            trvParent.SelectedNode = tempNode[0];
                        }
                    }
                    
                }
            }
            catch (NullReferenceException ex)
            {
                throw new Exception(ex.Message);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion
        #region "Function to move a node of the treeview Left"
        public void MoveLeft()
        {
             try
            {
                if (trevViewNode == null)
                    throw new NullReferenceException("The Property 'TreeViewNode' hasn't been assigned. Please assing the treeview you want to operate on.");
                else
                {
                    if (trevViewNode.Level >= 2)
                    {
                        TreeNode tempNode = (TreeNode)trevViewNode.Clone();
                        if (trevViewNode.Parent.NextNode != null)
                            trevViewNode.Parent.Parent.Nodes.Insert(trevViewNode.Parent.NextNode.Index, tempNode);
                        else
                            trevViewNode.Parent.Parent.Nodes.Add(tempNode);
                        trevViewNode.Remove();
                        //select the required node
                        trvParent.SelectedNode = tempNode;
                    }
                    else if (trevViewNode.Level == 1)                       
                    {
                        TreeNode tempNode = (TreeNode)trevViewNode.Clone();
                        if (trevViewNode.Parent.NextNode != null)
                            trevViewNode.TreeView.Nodes.Insert(trevViewNode.Parent.NextNode.Index, tempNode);
                        else
                            trevViewNode.TreeView.Nodes.Add(tempNode);
                        trevViewNode.Remove();
                        //select the node
                        trvParent.SelectedNode = tempNode;
                    }
                    //for level 0 nodes, no left movement.
                }
            }
            catch (NullReferenceException ex)
            {
                throw new Exception(ex.Message);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion
    }
    #endregion
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
Sid loves programming and has technical experience developing desktop based solutions using C#.Net, Winforms, WPF.
He has experience of Software services, I-Banks and Product development environments.
He also has a deep understanding of Product Development Lifecycle and Agile methodology of developing softwares.

Besides programming he is also fond of music, photography and cooking.
He lives in Bangalore.

Comments and Discussions