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

Author: Zunnair
Download Source Code : 900_control.zip

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

 

Remove:
If you want to remove the particular node for this purpose dot net provides us a function named remove (). This function takes tree node object as a parameter and returns nothing.
To demonstrate make a window application drag 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_delNode_Click(object sender, EventArgs e)
        {
            TreeNode obj = new TreeNode();
            string name = string.Empty;
            string text = string.Empty;
            string tag = string.Empty;
            name = txt_delname.Text;
            text = txt_deltext.Text;
            tag = txt_delTag.Text;
            if (name != string.Empty && text != string.Empty && tag != string.Empty)
            {
                obj.Name = name;
                obj.Text = text;
                obj.Tag = tag;
                treeView1.Nodes.RemoveAt(0);
                MessageBox.Show("Node Dleted" + treeView1.Nodes.Count.ToString());
            }
            else
                MessageBox.Show("add all values");
        }
VB
Private Sub btn_delNode_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New TreeNode()
        Dim name As String = String.Empty
        Dim text As String = String.Empty
        Dim tag As String = String.Empty
        name = txt_delname.Text
        text = txt_deltext.Text
        tag = txt_delTag.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.RemoveAt(0)
            MessageBox.Show("Node Dleted" & treeView1.Nodes.Count.ToString())
        Else
            MessageBox.Show("add all values")
        End If
    End Sub
This simple code deletes a node in tree control view. But there is a problem, if you want to use this technique so you have to enter all the values of a node which would be difficult.
So the better option is to select the node at run time and delete it for this purpose we’ll use the same function but we have to use a property which will tell that which node is selected.
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_delNode_Click(object sender, EventArgs e)
        {
            TreeNode obj = new TreeNode();
            obj = treeView1.SelectedNode;
            treeView1.Nodes.Remove(obj);
            MessageBox.Show("Node deleted");
        }
VB
Private Sub btn_delNode_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New TreeNode()
        obj = treeView1.SelectedNode
        treeView1.Nodes.Remove(obj)
        MessageBox.Show("Node deleted")
    End Sub
This simple code deletes a node in tree control view which is selected at run time.
Remove At:
If you want to remove the particular node by its index for this purpose we have a function named remove at () this function takes the index of node in integer form as a parameter and returns nothing. Index starts from zero.
To demonstrate make a window application drag a tree view control, text box and button on the form. Write index in text box and press button.
Now write the following code on button click event:
C#
private void btn_delindex_Click(object sender, EventArgs e)
        {
            string index = string.Empty;
            int id = 0;
            int count = 0;
            index = txt_delindex.Text;
            id = Convert.ToInt16(index);
            count=treeView1.Nodes.Count;
            count--;
            if(id<=count && id>=0)
            {
            treeView1.Nodes.RemoveAt(id);
            MessageBox.Show("Node Deleted");
       
            }
            else
                MessageBox.Show("Index Out of Range");
        }
VB
Private Sub btn_delindex_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim index As String = String.Empty
        Dim id As Integer = 0
        Dim count As Integer = 0
        index = txt_delindex.Text
        id = Convert.ToInt16(index)
        count = treeView1.Nodes.Count
        count -= 1
        If id <= count AndAlso id >= 0 Then
            treeView1.Nodes.RemoveAt(id)
 
            MessageBox.Show("Node Deleted")
        Else
            MessageBox.Show("Index Out of Range")
        End If
    End Sub
This simple code deletes a tree node by its index in tree view control.
Remove All:
If you want to remove all the nodes in tree view control. We have a function named clear ().This function takes and returns nothing.
To demonstrate make a window application drag a tree view control and button on the form. Press button.
 
C#
private void btn_delall_Click(object sender, EventArgs e)
        {
            treeView1.Nodes.Clear();
            string str=string.Empty;
            str = "Tree is Empty";
            MessageBox.Show(str);
        }
VB
Private Sub btn_delall_Click(ByVal sender As Object, ByVal e As EventArgs)
        treeView1.Nodes.Clear()
        Dim str As String = String.Empty
        str = "Tree is Empty"
        MessageBox.Show(str)
    End Sub
This simple code deletes all the nodes 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 remove a particular tree node and all the tree nodes in “Tree View Control” in VB and C# .net 2.0.
Article Comments
When i remove object(treeview1.selectedNode) then treeview1 not remove node.
Please help me ..
Thank you.

my code write by Asp.Net ..

Protected Sub DetailsView1_ItemDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewDeleteEventArgs) Handles DetailsView1.ItemDeleting

Dim node As TreeNode = TreeView1.SelectedNode
TreeView1.Nodes.Remove(node)

End Sub

Posted on 8/4/2009 10:15:41 PM by Sorn

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

Disclaimer - Privacy
© 2002-2017 DevASP.net