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
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:
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:
Choose the Browse TAB and navigate to the ABS.TreeNodeNavigator.dll:
The next step involves adding this user directive in your code to avoid writing the derived name of the class:
using ABS.TreeNodeNavigator;
Declare an object of the class:
Navigator trv = new Navigator();
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:
Move Up
trv.TreeViewNode = this.treeView1.SelectedNode;
trv.MoveUp();
Move Down
trv.TreeViewNode = this.treeView1.SelectedNode;
trv.MoveDown();
Move Right
trv.TreeViewNode = this.treeView1.SelectedNode;
trv.MoveRight();
Move Left
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:
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?
I have followed the following logic while writing the class:
- Move Up: Interchanging position with the sibling above
- Move Down: Interchanging position with the sibling below
- Move Right: Becoming child of the sibling above
- 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