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 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).

Author: Zunnair
Download Source Code : 902_control.zip

In this simple article you will learn that how to get total number of nodes, index of a node, tree view contains a node and selected tree node in “Tree View Control” in VB and C# .net 2.0.

 

Selected:
If you want to know that which node is selected for this purpose we have a property named selected node. This property returns an object of tree node class which contains the information about the selected node.
To demonstrate make a window application drag tree view control, and button on the form.
Now write the following code on button click event:
C#
        private void btn_Selected_Click(object sender, EventArgs e)
        {
            TreeNode obj = new TreeNode();
            obj = treeView1.SelectedNode;
            MessageBox.Show(obj.Text);
        }
VB
Private Sub btn_Selected_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New TreeNode()
        obj = treeView1.SelectedNode
        MessageBox.Show(obj.Text)
    End Sub
This simple code tells about the selected node.
Count:
If you want to count the total number of nodes for this purpose dot net provides us a function named count (). This function takes nothing and returns an integer this integer tells the total number of nodes which tree view control has in it.
To demonstrate make a window application drag tree view control, and button on the form. Press the button and you will get the total number of nodes.
Now write the following code on button click event:
C#
private void bnt_Count_Click(object sender, EventArgs e)
        {
            int count;
            count = treeView1.Nodes.Count;
            string str = string.Empty;
            str = count.ToString();
            MessageBox.Show("Total nodes Are " + str);
        }
VB
Private Sub bnt_Count_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim count As Integer
        count = treeView1.Nodes.Count
        Dim str As String = String.Empty
        str = count.ToString()
        MessageBox.Show("Total nodes Are " & str)
    End Sub
This simple code counts the number of nodes which are added in tree view control.
Index of Nodes:
If you want to get the index of a particular node in tree view control for this purpose dot net provides us a function named index of (). This function takes the object of tree node class about which you want to know the index. This function returns the integer which shows the index.
To demonstrate make a window application drag tree view control and button on the form. Select the node and press button.
Now write the following code on button click event:
C#
private void btn_indexof_Click(object sender, EventArgs e)
        {
            int index;
            TreeNode obj = new TreeNode();
            obj = treeView1.SelectedNode;
            index = treeView1.Nodes.IndexOf(obj);
            string str=string.Empty;
            str = index.ToString();
            MessageBox.Show("index of selected tree node is " + str);
        }       
VB
Private Sub btn_indexof_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim index As Integer
        Dim obj As New TreeNode()
        obj = treeView1.SelectedNode
        index = treeView1.Nodes.IndexOf(obj)
        Dim str As String = String.Empty
        str = index.ToString()
        MessageBox.Show("index of selected tree node is " & str)
    End Sub
This simple code tells the index of a node in tree control view which is selected at run time.
Contains:
If you want to know that particular node exist in tree view control or not for this purpose dot net provides a function named contains ().This function takes a tree node class object as a parameter and returns a Boolean value. If it returns true it means node exists else not exist.
To demonstrate make a window application drag tree view control and button on the form. Select the node and press button.
Now write the following code on button click event:
C#
private void btn_contains_Click(object sender, EventArgs e)
        {
            TreeNode obj = new TreeNode();
            obj = treeView1.SelectedNode;
            bool b;
            b = treeView1.Nodes.Contains(obj);
            string str = string.Empty;
            str = b.ToString();
            MessageBox.Show(str);
        }       
VB
Private Sub btn_contains_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New TreeNode()
        obj = treeView1.SelectedNode
        Dim b As Boolean
        b = treeView1.Nodes.Contains(obj)
        Dim str As String = String.Empty
        str = b.ToString()
        MessageBox.Show(str)
    End Sub
This simple code tells that the particular node exist in tree view control or not.
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 get total number of nodes, index of a node, tree view contains a node and selected tree node in “Tree View Control” in VB and C# .net 2.0.
Article Comments
this is benifited to me


thanks

Posted on 9/27/2010 6:15:51 AM by vishranti

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to remove a particular tree node and all tree nodes in “Tree View Control” in VB and C# (.net 2.0).

Disclaimer - Privacy
© 2002-2017 DevASP.net