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 add a tree node, and tree node collection in “Tree View Control” in VB and C# (.net 2.0).

Author: Zunnair
Download Source Code : 899_control.zip

In this simple article you will learn that how to add a tree node and tree node collection in “Tree View Control” in VB and C# .net 2.0.

 

Tree view Control:
Tree view is control which is provided by dot net. In this control you can add multiple nodes and their child. You can make a tree view of your application.
Add Tree Node:
First of it is important to know how we can add a tree node in a tree view control. To add a tree node, first we have to node to make a node. For this purpose dot net provides us a class name “Tree Node”. This class has different properties and functions, which I have discussed in my previous articles. To understand the nodes read that articles.
Tree view control has a collection of nodes. To add the node we have a function named add (). This function has different overloaded method.
First Method:
The first method takes the string as a parameter. This function shows that string as a node in tree view control and does not returns anything.
To demonstrate make a window application drag tree view control, text box and button on the form. Write the text in text box and press the button.
Now write the following code on button click event:
C#
private void button1_Click(object sender, EventArgs e)
        {
            string text = string.Empty;
            text = txt_TextStr.Text;
            if (text != string.Empty)
            {               
                treeView1.Nodes.Add(text);
            }
            else
                MessageBox.Show("add values in text box");
        }
VB
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim text As String = String.Empty
        text = txt_TextStr.Text
        If text <> String.Empty Then
            treeView1.Nodes.Add(text)
        Else
            MessageBox.Show("add values in text box")
        End If
    End Sub
This simple code adds the string as a node in tree control view.
Second Method:
This function takes object of tree node class as a parameter and returns nothing. This object contains all the information about the node. To add multiple nodes you will have to create different objects of tree node class. Or create an object again and again and then add it in tree view control. You can also use an array for this purpose.
To demonstrate make a window application drag a tree view control, three text boxes and button on the form. Write the text, name and tag in text boxes and press the button.
Now write the following code on button click event:
C#
private void btn_AddTreeNode_Click(object sender, EventArgs e)
        {
            TreeNode obj = new TreeNode();
            obj.BackColor = Color.Aqua;
            string name=string.Empty;
            string text = string.Empty;
            string tag = string.Empty;
            name = txt_name.Text;
            text = txt_text.Text;
            tag = txt_Tag.Text;
            if (name != string.Empty && text != string.Empty && tag != string.Empty)
            {
                obj.Name = name;
                obj.Text = text;
                obj.Tag = tag;
                treeView1.Nodes.Add(obj);
            }
            else
                MessageBox.Show("add all values");
        }
VB
Private Sub btn_AddTreeNode_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New TreeNode()
        obj.BackColor = Color.Aqua
        Dim name As String = String.Empty
        Dim text As String = String.Empty
        Dim tag As String = String.Empty
        name = txt_name.Text
        text = txt_text.Text
        tag = txt_Tag.Text
        If name <> String.Empty AndAlso text <> String.Empty AndAlso tag <> String.Empty Then
            obj.Name = name
            obj.Text = text
            obj.Tag = tag
            treeView1.Nodes.Add(obj)
        Else
            MessageBox.Show("add all values")
        End If
    End Sub
This simple code adds a node in tree control view.
Add Tree Node Array:
If you want to add an array in tree view control for this purpose dot net provides us a function named add range (). This function takes array of tree node class and returns nothing. Make the array and add the values in array. Array should be full before passing in the function other wise there will be an exception. After adding the values call the function.
To demonstrate make a window application drag a tree view control, three text boxes and two button on the form. Write the text, name and tag in text boxes and press the button to add in array press again and again till the array does not get full. Then press button to put the array in control.
To add the values in array
Now write the following code on button click event:
C#
private void btn_AddArray_Click(object sender, EventArgs e)
        {
            TreeNode obj1 = new TreeNode();
            string name = string.Empty;
            string text = string.Empty;
            string tag = string.Empty;
            name = txt_ArrayName.Text;
            text = txt_ArrayText.Text;
            tag = txt_ArrayTag.Text;
            if (name != string.Empty && text != string.Empty && tag != string.Empty)
            {
                if (i <= 2)
                {
                    obj1.Name = name;
                    obj1.Text = text;
                    obj1.Tag = tag;
                    Array.SetValue(obj1, i);
                    i++;
                }
                else
                    MessageBox.Show("Array is full");
 
            }
            else
                MessageBox.Show("Add all values");
 
        }
VB
Private Sub btn_AddArray_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj1 As New TreeNode()
        Dim name As String = String.Empty
        Dim text As String = String.Empty
        Dim tag As String = String.Empty
        name = txt_ArrayName.Text
        text = txt_ArrayText.Text
        tag = txt_ArrayTag.Text
        If name <> String.Empty AndAlso text <> String.Empty AndAlso tag <> String.Empty Then
            If i <= 2 Then
                obj1.Name = name
                obj1.Text = text
                obj1.Tag = tag
                Array.SetValue(obj1, i)
                i += 1
            Else
                MessageBox.Show("Array is full")
 
            End If
        Else
            MessageBox.Show("Add all values")
 
        End If
    End Sub
This simple code adds the values in tree view control.
To add the array in tree view control.
C#
private void btn_AddTree_Click(object sender, EventArgs e)
        {
                if (i == 3)
                    treeView1.Nodes.AddRange(Array);
                else
                    MessageBox.Show("Array is not full add values");
        }
VB
Private Sub btn_AddTree_Click(ByVal sender As Object, ByVal e As EventArgs)
        If i = 3 Then
            treeView1.Nodes.AddRange(Array)
        Else
            MessageBox.Show("Array is not full add values")
        End If
    End Sub
This simple code adds the array in tree view control.
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 to add a tree node and tree node collection in “Tree View Control” in VB and C# .net 2.0.
   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to make tree node (Expand, Expand All, count, toggle, remove) using “Tree Node class” in VB and C# (.net 2.0). Part 5

Disclaimer - Privacy
© 2002-2017 DevASP.net