Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to make tree node (Name, Text, Tag, First Node, Parent) using “Tree Node class” in VB and C# (.net 2.0). Part 1

Author: Zunnair
Download Source Code : 892_Tree Node.zip

In this simple article you will learn that how we can make a tree node and set node properties using “Tree Node class” in VB and C# .net 2.0.

 

What is Tree Node?
As we know that tree has some branches and each branch can have more branches. From the data structure point of view tree is structure which has some nodes and very node has some properties. In programming there are many types of trees but that will not be discussed in this article because this article is about the nodes and there properties.
 For making a node dot net provides us a class named tree node.
Declaring an Object:
To declare an object of tree node class write the following code.
C#
TreeNode obj = new TreeNode();
VB
Dim obj As New TreeNode
There are five overloaded constructors are available.
Properties of a Node:
There are some properties which you have to define for making a node.
Name:
Every node has its name so it can help to find the node. To set or get the name of a node tree node class provides us a property named Name. this property takes or returns a string.
Set:
Write the following code.
C#
string str = "Main Node";
obj.Name = str;
VB
Dim str As String = "Main Node"
obj.Name = str
This simple code sets the name of node.
Get:
Write the following code.
C#
string str = obj.Name;
MessageBox.Show(str);
VB
Dim str As String = obj.Name
MessageBox.Show(str)
This simple code gets the name of node.
Text:
Text property is used to set or get the name which should be shown to user in the Tree View control. Actually these nodes are inserted in tree view control which I have discussed in my other articles.
Set:
Write the following code.
C#
string str = "First";
obj.Text = str;
VB
Dim str As String = "First"
obj.Text = str
This simple code sets the Text of node.
Get:
Write the following code.
C#
string str = obj.Text;
MessageBox.Show(str);
VB
Dim str As String = obj.Text
MessageBox.Show(str)
This simple code gets the text of node.
Tag:
A tag is attached with every node which helps to get the data about node. For this we have a property named tag this property takes object and returns object which contains data.
Set:
Write the following code.
C#
object obj1 = new object();
obj.Tag = obj1;
VB
Dim obj1 As New Object()
obj.Tag = obj1
This simple code sets the Tag of node.
Get:
Write the following code.
C#
object obj1 = new object();
obj.Tag = obj1;
string str = obj.Text;
MessageBox.Show(str);
VB
Dim str As String = obj.Text
Dim obj1 As New Object()
Obj1= obj.Tag
str=obj1.ToString()
MessageBox.Show(str)
This simple code gets the tag of node.
 
First Node:
A node can have children, if a node has some children and we want to get the first child node so we have to use the property named first node. This property returns an object of tree node class. This contains the information about first child tree node.
Now write the following code.
C#
TreeNode o = new TreeNode();
o = obj.FirstNode;
MessageBox.Show(o.Text);
VB
Dim o As New TreeNode()
o = obj.FirstNode
MessageBox.Show(o.Text)
This simple code gets the first child of node.
Parent:
Every Node has a parent except the root node. To get the parent of the node we have a property named parent. This property returns the object of tree node class. This node contains the information about parent node.
Write the following code.
C#
TreeNode p = new TreeNode();
p = obj.Parent;
MessageBox.Show(p.Text);
VB
Dim p As New TreeNode()
p = obj.Parent
MessageBox.Show(p.Text)
This simple code gets the parent of the node.
Now write the following code on FORM LOAD event:
C#
private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "DEVASP TREE NODE APPLICATION";
        }     
VB
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
        Me.Text = "DEVASP TREE NODE APPLICATION"
    End Sub
 
This simple article tells that how we can make a tree node and set node properties using “Tree Node class” in VB and C# .net 2.0.

 

Article Comments
thank you very much. If I want use rad control then how can I build it?

Posted on 9/29/2010 12:47:55 PM by Aftab

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to use Month Calendar Control in VB and C# (.net 2.0). Part 4

Disclaimer - Privacy
© 2002-2017 DevASP.net