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

Author: Zunnair
Download Source Code : 904_control.zip

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

 

Insert Tree Node:
Tree view control has a collection of nodes. To insert the node we have a function named insert (). This function has different overloaded method.
First Method:
The first method takes the string as a parameter and the index. This function inserts that string as a node in tree view control at given index 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, select the tree node and press the button.
Now write the following code on button click event:
C#
private void btn_String_Click(object sender, EventArgs e)
        {
            TreeNode obj1 = new TreeNode();
            int ind = 0;
            string str = string.Empty;
            str = txt_string.Text;
            if (str != string.Empty)
            {
                obj1 = treeView1.SelectedNode;
                ind = obj1.Index;
                treeView1.Nodes.Insert(ind, str);
            }
            else
                MessageBox.Show("add value");
        }
VB
Private Sub btn_String_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj1 As New TreeNode()
        Dim ind As Integer = 0
        Dim str As String = String.Empty
        str = txt_string.Text
        If str <> String.Empty Then
            obj1 = treeView1.SelectedNode
            ind = obj1.Index
            treeView1.Nodes.Insert(ind, str)
        Else
            MessageBox.Show("add value")
        End If
    End Sub
This simple code inserts the string as a node in tree control view.
Second Method:
This function takes object of tree node class and index as parameters and returns nothing. This object contains all the information about the node. To insert multiple nodes you will have to create different objects of tree node class. Or create an object again and again and then insert 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, select node and press the button.
Now write the following code on button click event:
C#
private void bnt_Insert_Click(object sender, EventArgs e)
        {
            TreeNode obj = new TreeNode();
            TreeNode obj1 = new TreeNode();
            int ind = 0;
            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;
                obj1 = treeView1.SelectedNode;
                ind = obj1.Index;
                treeView1.Nodes.Insert(ind, obj);
            }
            else
                MessageBox.Show("add all values");
 
        }
VB
Private Sub bnt_Insert_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New TreeNode()
        Dim obj1 As New TreeNode()
        Dim ind As Integer = 0
        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
            obj1 = treeView1.SelectedNode
            ind = obj1.Index
            treeView1.Nodes.Insert(ind, obj)
        Else
            MessageBox.Show("add all values")
 
        End If
    End Sub
This simple code inserts a node in tree control view.
Top Node:
To get the top node of the tree view control we have a property named Top Node. This property returns the tree node class object. This object contains the information about the top node.
To demonstrate make a window application drag tree view control and button on the form. Press the button.
Now write the following code on button click event:
C#
private void btn_Parent_Click(object sender, EventArgs e)
        {
            TreeNode obj = new TreeNode();
            obj = treeView1.TopNode;
            string str;
            str = obj.Text;
            MessageBox.Show(str);
        }
VB
Private Sub btn_Parent_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New TreeNode()
        obj = treeView1.TopNode
        Dim str As String
        str = obj.Text
        MessageBox.Show(str)
    End Sub
This simple code gets the top node in tree view control.
Now write the following code on FORM LOAD event:
C#
private void Form1_Load(object sender, EventArgs e)
        {
            TreeNode obj = new TreeNode();
            TreeNode obj1 = new TreeNode();
            TreeNode obj2 = new TreeNode();
            this.Text = "Devasp Tree View Control Application";
            obj.Name = "a";
           obj.Text = "a";
            obj.Tag = "1";
            obj1.Name = "b";
            obj1.Text = "b";
            obj1.Tag = "2";
            obj2.Name = "c";
            obj2.Text = "c";
            obj2.Tag = "3";
            treeView1.Nodes.Add(obj);
            treeView1.Nodes.Add(obj1);
            treeView1.Nodes.Add(obj2);
        }
VB
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New TreeNode()
        Dim obj1 As New TreeNode()
        Dim obj2 As New TreeNode()
        Me.Text = "Devasp Tree View Control Application"
        obj.Name = "a"
        obj.Text = "a"
        obj.Tag = "1"
        obj1.Name = "b"
        obj1.Text = "b"
        obj1.Tag = "2"
        obj2.Name = "c"
        obj2.Text = "c"
        obj2.Tag = "3"
        treeView1.Nodes.Add(obj)
        treeView1.Nodes.Add(obj1)
        treeView1.Nodes.Add(obj2)
    End Sub
 
This simple article tells that how to insert a tree node and how to get top node in “Tree View Control” in VB and C# .net 2.0.
   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to get total number of nodes, index of node, tree view contains node and selected tree node in “Tree View Control” in VB and C# (.net 2.0).

Disclaimer - Privacy
© 2002-2017 DevASP.net