Click here to Skip to main content
15,880,543 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.3K   889   19   3
The class presented here allows the user to navigate the TreeNodes in a TreeView control


Of all the controls provided by the .NET SDK, TreeView perhaps is the most difficult to tame. However no matter how much you try finding out alternative ways, an encounter with the control is inevitable.

The class presented here provides an abstraction layer for navigating the nodes. The class (compiled into a DLL) may be included in your project reference and then just a function call may serve the purpose of navigating the selected node.

On a personal note: I am not much of a professional programmer nor am I backed with years of experience. I haven't written the article with the view of teaching a new concept altogether. For that, there are some real cool guys out here. I consider myself a little above beginners’ level. My aim is just to share something that may save time for some developers and thus contribute to this wonderful community here. The code presented may not be optimum; I sincerely invite your comments so that I may keep the article updated.

Knowing the Navigator Class

I wonder if such a small class has ever been presented!


  • TreeViewNode: The only property of the class to take the node which is to be navigated.


There are four methods for performing the four desired tasks:

  • MoveUp()
  • MoveDown()
  • MoveRight()
  • MoveLeft()

All the methods return void and none of them takes any parameter.

Using the DLL

  1. A year ago, I did not know what adding a reference meant. For those who are on the same step of the ladder, follow (climb) the following two STEPS:

    1. Considering that you have downloaded the code with this article, right clicking on the References node of your project will present two items.
      Choose Add Reference item:


    2. Choose the Browse TAB and navigate to the ABS.TreeNodeNavigator.dll:


  2. The next step involves adding this user directive in your code to avoid writing the derived name of the class:

    using ABS.TreeNodeNavigator;
  3. Declare an object of the class:

    Navigator trv = new Navigator();
  4. Your application is now ready for using the Navigator class. There are four things that you may want to do. I write the code required for achieving them:

    1. Move Up

      trv.TreeViewNode = this.treeView1.SelectedNode;
    2. Move Down

      trv.TreeViewNode = this.treeView1.SelectedNode;
    3. Move Right

      trv.TreeViewNode = this.treeView1.SelectedNode;
    4. Move Left

      trv.TreeViewNode = this.treeView1.SelectedNode;

I think this is pretty simple to understand and bring in practice. However, there are a couple of things that you may like to note:

  1. If you are assigning the property TreeViewNode of the object to a TreeNode object that is not linked with a treeview, your code may throw the following exception:


    The class believes in the philosophy - If no tree is there, on what will the node navigate?

  2. I have followed the following logic while writing the class:

    1. Move Up: Interchanging position with the sibling above
    2. Move Down: Interchanging position with the sibling below
    3. Move Right: Becoming child of the sibling above
    4. Move Left: Becoming sibling of the parent

If you have noted, the Move Up-Move Down and Move Right-Move Left are logical negations of each other. E.g. if a node becomes child of the above sibling, then undoing the action would require becoming sibling of the parent. Therefore, the methods provided may be exploited for undoing each other's job using a flag.

Maybe you would like to come up with different logic. However, there must be in place, a logical pairing like the one mentioned above.

Below the Abstraction Layer

The class can be easily followed by someone at the beginner level. The methods work on the property TreeViewNode of the Navigator class.


  • 19th August, 2008: Initial post


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

QuestionWonderful! Pin
Francesco Giossi6-May-14 0:08
Francesco Giossi6-May-14 0:08 
GeneralSuggestion for improvement Pin
Sinisa Hajnal26-Aug-08 0:10
professionalSinisa Hajnal26-Aug-08 0:10 
GeneralRe: Suggestion for improvement [modified] Pin
Siddhartha S.26-Aug-08 17:31
Siddhartha S.26-Aug-08 17:31 
Thanks Sinia. I will work upon this.
I just wanted to display the use of property also. Thats why I opted for this procedure. I could have also passed the treeNode in the class constructor itself. That would have made it more logical.
However, changing the function signature, as suggested by you is also a good option.


modified on Tuesday, August 26, 2008 11:40 PM

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.