Click here to Skip to main content
13,356,787 members (81,857 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


18 bookmarked
Posted 19 Aug 2008

Tree Nodes Navigator

, 19 Aug 2008
Rate this:
Please Sign up or sign in to vote.
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)


About the Author

Siddhartha S.
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 Mumbai with his wife.

You may also be interested in...

Comments and Discussions

QuestionWonderful! Pin
Francesco Giossi6-May-14 1:08
memberFrancesco Giossi6-May-14 1:08 
GeneralSuggestion for improvement Pin
Sinia Hajnal26-Aug-08 1:10
memberSinia Hajnal26-Aug-08 1:10 
GeneralRe: Suggestion for improvement [modified] Pin
babai2826-Aug-08 18:31
memberbabai2826-Aug-08 18:31 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 20 Aug 2008
Article Copyright 2008 by Siddhartha S.
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid