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

Author: Zunnair
Download Source Code : 905_control.zip

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

 

Three Types:

Here I shall discuss three ways to find a node.

1.       Find by Name

2.       Find by Text

3.       Find by tag

As I told about the properties of a tree node and a node contains name, text and tag. We can search a node in tree view control by name, text and tag.

To understand the properties read my previous articles.

Find By Name:

To find a node in tree view control, we have a function named find (). This function takes two parameters one is string, name of node which you decide as a key to find the node. Second parameter is a Boolean value. This Boolean value decides that function should check the child of a node or not. If its true, it means check the child else do not check. A tree view can contain a multiple nodes of same name so this function returns all the nodes of same name as an array of tree node class. So array contains all the nodes of same name.

To demonstrate make a window application drag tree view control, text box and button on the form. Write name in text box and press the button.

Now write the following code on button click event:

C#

private void bnt_SearchName_Click(object sender, EventArgs e)

        {

            string name=string.Empty;

            name = txt_Name.Text;

            bool b = true;

            if (name != string.Empty)

            {

                try

            {

                TreeNode[] arr = treeView1.Nodes.Find(name,b);

               

                for (i = 0; i < arr.Length; i++)

                {

                    treeView1.SelectedNode = arr[i];

                    treeView1.SelectedNode.BackColor = Color.Red;

                }

            }

            catch { }

       

            }

            else

                MessageBox.Show("Enter Name");

    }

VB

Private Sub bnt_SearchName_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim name As String = String.Empty

        name = txt_Name.Text

        Dim b As Boolean = True

        If name <> String.Empty Then

            Try

                Dim arr As TreeNode() = treeView1.Nodes.Find(name, b)

 

                For i = 0 To arr.Length - 1

                    treeView1.SelectedNode = arr(i)

                    treeView1.SelectedNode.BackColor = Color.Red

                Next

            Catch

 

            End Try

        Else

            MessageBox.Show("Enter Name")

        End If

    End Sub

This simple code finds node by its name in tree control view.

Find By Text:

Same function is used to find by text. You will have to pass a text value of tree node and Boolean value as a parameter.

To demonstrate make a window application drag tree view control, text box and button on the form. Write text in text box and press the button.

Now write the following code on button click event:

C#

private void btn_SearchText_Click(object sender, EventArgs e)

        {

            string text = string.Empty;

            text = txt_Text.Text;

            bool b = true;

            if (text != string.Empty)

            {

                try

                {

                    TreeNode[] arr = treeView1.Nodes.Find(text, b);

                    for (i = 0; i < arr.Length; i++)

                    {

                        treeView1.SelectedNode = arr[i];

                        treeView1.SelectedNode.BackColor = Color.Blue;

                    }

                }

                catch { }

 

            }

            else

                MessageBox.Show("Enter Text");

        }

VB

Private Sub btn_SearchText_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim text As String = String.Empty

        text = txt_Text.Text

        Dim b As Boolean = True

        If text <> String.Empty Then

            Try

                Dim arr As TreeNode() = treeView1.Nodes.Find(text, b)

                For i = 0 To arr.Length - 1

                    treeView1.SelectedNode = arr(i)

                    treeView1.SelectedNode.BackColor = Color.Blue

                Next

            Catch

 

            End Try

        Else

            MessageBox.Show("Enter Text")

        End If

    End Sub

This simple code finds node by its text in tree control view.

Find By Tag:

To find by a tag first get the all the nodes in tree node collection object than one by one get the tab of each node and compare it with the entered tag value. If matched than display found else not found.

To demonstrate make a window application drag tree view control, text box and button on the form. Write tag in text box and press the button.

Now write the following code on button click event:

C#

private void btn_SearchTag_Click(object sender, EventArgs e)

        {

            string tag = string.Empty;

            tag = txt_Tag.Text;

            int j = 0;

            if (tag != string.Empty)

            {

                TreeNodeCollection tnc;

                TreeNode[] arr = new TreeNode();

                tnc = treeView1.Nodes;

                for (i = 0; i < treeView1.Nodes.Count; i++)

                {

                    if (tag ==(string) tnc[i].Tag)

                    {

                        tnc[i].BackColor = Color.Pink;

                        j = 1;

                    }

                }

                if(j==0)

                MessageBox.Show("not found");

            }

            else

                MessageBox.Show("Enter Tag");

        }

VB

Private Sub btn_SearchTag_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim tag As String = String.Empty

        tag = txt_Tag.Text

        Dim j As Integer = 0

        If tag <> String.Empty Then

            Dim tnc As TreeNodeCollection

            Dim arr As TreeNode() = New TreeNode()

            tnc = treeView1.Nodes

            For i = 0 To treeView1.Nodes.Count - 1

                If tag = DirectCast(tnc(i).Tag, String) Then

                     tnc[i].BackColor = Color.Pink;

                    j = 1

                End If

            Next

            If j = 0 Then

                MessageBox.Show("not found")

            End If

        Else

            MessageBox.Show("Enter Tag")

        End If

    End Sub

This simple code finds node by its tag in tree control view.

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

Article Comments
Thank you for this article.

Posted on 10/28/2009 9:17:24 AM by Ishwar

Thank You

Posted on 12/7/2009 6:26:28 AM by masuki

FANTASTIC ARTICLE. KEEP IT UP

Posted on 1/21/2010 1:16:42 AM by arul

its okey. thank you

Posted on 4/28/2010 4:24:50 PM by cem

Thank you so much. It's very helpful,keep it up

Posted on 5/29/2010 1:48:35 PM by Tu?n Cu?ng

Your find by text does not work as it does exactly the same as find by name. TreeView.Find only searches for the name of the node

Posted on 6/22/2010 1:49:42 PM by Ap

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

Disclaimer - Privacy
© 2002-2017 DevASP.net