VB.NET Technology

VB.Net Projects

VB.Net Project 1

TreeView Control
Previous Home Next
adplus-dvertising

TreeView Control is used for showing hierarchical structured data visually e.g.. XML.

Example

This is image of Explorer in Windows that show files and folder in tree view.

Working With TreeView Control

Drag and drop TreeView Control from toolbox, Like shown in below image.

In following example I have taken a tree list view of colleges name for this i have taken ContextMenuChild and Parent for adding nodes. And you can also add by on clicking add child button and add sibling event.

Code for Add Child is given Below

Private Sub buttonAddChild_Click(ByVal sender As System.Object, 
	ByVal e As System.EventArgs) Handles buttonAddChild.Click
        If textBox1.Text <> "" Then
            AddChildToTheNode()
        Else
            MessageBox.Show("Enter the Node Text to be added")
            textBox1.Focus()
        End If
    End Sub
Private Sub AddChildToTheNode()
        Dim tnode As New TreeNode(textBox1.Text)
        treeView1.SelectedNode.Nodes.Add(tnode)
        treeView1.ExpandAll()
        If treeView1.SelectedNode.Nodes.Count > 1 
	AndAlso treeView1.SelectedNode.ForeColor <> Color.Blue Then
            treeView1.SelectedNode.ForeColor = Color.Brown
        End If
    End Sub

Code for Menu Item Click

If textBox1.Text <> "" Then
	AddChildToTheNode()
Else
	MessageBox.Show("Enter the Node Text to be added")
	textBox1.Focus()
End If

Code for Add Sibling

Private Sub btnAddSibling_Click(ByVal sender As System.Object, 
	ByVal e As System.EventArgs) Handles btnAddSibling.Click
        If textBox1.Text <> "" Then
            AddSiblingToTheNode()
        Else
            MessageBox.Show("Enter the Node Text to be added")
            textBox1.Focus()
        End If
    End Sub
Private Sub AddSiblingToTheNode()
        Dim tnode As New TreeNode(textBox1.Text)
        tnode.ForeColor = Color.Brown
        treeView1.SelectedNode.Parent.Nodes.Add(tnode)
    End Sub

Code For Deleting Child and Sibling.

Private Sub buttonDeleteParent_Click(ByVal sender As System.Object, 
	ByVal e As System.EventArgs) Handles buttonDeleteParent.Click
        DeleteNode()
    End Sub
    Private Sub DeleteNode()
        If treeView1.SelectedNode.Nodes.Count = 0 Then
            treeView1.SelectedNode.Remove()
        Else
            MessageBox.Show("First Remove all the child nodes")
        End If
    End Sub
Previous Home Next