Click here to Skip to main content
15,867,568 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
ScreenShot.PNG

Introduction

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!

Properties

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

Methods

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:

      AddingRefernce.PNG

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

      SelectingDll.PNG

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

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

    C#
    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

      C#
      trv.TreeViewNode = this.treeView1.SelectedNode;
      trv.MoveUp();
    2. Move Down

      C#
      trv.TreeViewNode = this.treeView1.SelectedNode;
      trv.MoveDown(); 
    3. Move Right

      C#
      trv.TreeViewNode = this.treeView1.SelectedNode;
      trv.MoveRight(); 
    4. Move Left

      C#
      trv.TreeViewNode = this.treeView1.SelectedNode;
      trv.MoveLeft(); 

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:

    Exception.PNG

    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.

History

  • 19th August, 2008: Initial post

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

 
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 
Simple and effective.

Consider, however, overloading all functions to receive TreeNode as parameter so you can dispense with assigning treeNode to Navigator before calling functions...
GeneralRe: Suggestion for improvement [modified] Pin
Siddhartha S.26-Aug-08 17:31
Siddhartha S.26-Aug-08 17: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.