Write a program to create a Treeview dynamically with buttons Add To Root, Add To Selected Node, Remove and Scan buttons.On click of Scan button all the nodes of the Treeview should be listed in the Listbox placed in side by.
CODING
Public Class Form1
Dim tnode As New TreeNode
Private Sub AddNodesToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AddNodesToolStripMenuItem.Click
If TextBox1.Text = "" Then
MsgBox("Enter Node Name in Right side Box")
Else
TreeView1.Nodes.Add(TextBox1.Text)
End If
End Sub
Private Sub SelectedNodeToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SelectedNodeToolStripMenuItem.Click
If TextBox1.Text = "" Then
MsgBox("Enter Node Name in Right side Box")
ElseIf TreeView1.SelectedNode Is Nothing Then
MsgBox("Select A Node")
Else
TreeView1.SelectedNode.Nodes.Add(TextBox1.Text)
End If
End Sub
Private Sub CountAllNodesToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CountAllNodesToolStripMenuItem.Click
MsgBox("Total Nodes " & TreeView1.Nodes.Count)
End Sub
Private Sub SelectedNodesToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SelectedNodesToolStripMenuItem.Click
If TreeView1.SelectedNode Is Nothing Then
MsgBox("Select A Node")
Else
MsgBox("Total Nodes " & TreeView1.SelectedNode.Nodes.Count)
End If
End Sub
Private Sub RemoveAllNodesToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RemoveAllNodesToolStripMenuItem.Click
TreeView1.Nodes.Clear()
End Sub
Private Sub RemoveSelectedNodesToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RemoveSelectedNodesToolStripMenuItem.Click
If TreeView1.SelectedNode Is Nothing Then
MsgBox("Select A Node")
Else
TreeView1.SelectedNode.Remove()
End If
End Sub
Private Sub ExpandAllNodesToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExpandAllNodesToolStripMenuItem.Click
TreeView1.ExpandAll()
End Sub
Private Sub ExpandSelectedNodesToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExpandSelectedNodesToolStripMenuItem.Click
If TreeView1.SelectedNode Is Nothing Then
MsgBox("Select A Node")
Else
TreeView1.SelectedNode.Expand()
End If
End Sub
Private Sub CollpaseAllNodesToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CollpaseAllNodesToolStripMenuItem.Click
TreeView1.CollapseAll()
End Sub
Private Sub CollpaseSelectedNodesToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CollpaseSelectedNodesToolStripMenuItem.Click
If TreeView1.SelectedNode Is Nothing Then
MsgBox("Select A Node")
Else
TreeView1.SelectedNode.Collapse()
End If
End Sub
Private Sub ScanneAllNodesToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ScanneAllNodesToolStripMenuItem.Click
For Each tnode In TreeView1.Nodes
Call scann_node(tnode)
Next
End Sub
Sub scann_node(ByVal tnode As TreeNode)
Static indentlevel As Integer
Application.DoEvents()
ListBox1.Items.Add(Space(indentlevel) & tnode.Text)
If tnode.Nodes.Count > 0 Then
indentlevel += 5
For Each tnode In tnode.Nodes
scann_node(tnode)
Next
indentlevel -= 5
End If
End Sub
End Class
DESIGNINGDim tnode As New TreeNode
Private Sub AddNodesToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AddNodesToolStripMenuItem.Click
If TextBox1.Text = "" Then
MsgBox("Enter Node Name in Right side Box")
Else
TreeView1.Nodes.Add(TextBox1.Text)
End If
End Sub
Private Sub SelectedNodeToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SelectedNodeToolStripMenuItem.Click
If TextBox1.Text = "" Then
MsgBox("Enter Node Name in Right side Box")
ElseIf TreeView1.SelectedNode Is Nothing Then
MsgBox("Select A Node")
Else
TreeView1.SelectedNode.Nodes.Add(TextBox1.Text)
End If
End Sub
Private Sub CountAllNodesToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CountAllNodesToolStripMenuItem.Click
MsgBox("Total Nodes " & TreeView1.Nodes.Count)
End Sub
Private Sub SelectedNodesToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SelectedNodesToolStripMenuItem.Click
If TreeView1.SelectedNode Is Nothing Then
MsgBox("Select A Node")
Else
MsgBox("Total Nodes " & TreeView1.SelectedNode.Nodes.Count)
End If
End Sub
Private Sub RemoveAllNodesToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RemoveAllNodesToolStripMenuItem.Click
TreeView1.Nodes.Clear()
End Sub
Private Sub RemoveSelectedNodesToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RemoveSelectedNodesToolStripMenuItem.Click
If TreeView1.SelectedNode Is Nothing Then
MsgBox("Select A Node")
Else
TreeView1.SelectedNode.Remove()
End If
End Sub
Private Sub ExpandAllNodesToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExpandAllNodesToolStripMenuItem.Click
TreeView1.ExpandAll()
End Sub
Private Sub ExpandSelectedNodesToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExpandSelectedNodesToolStripMenuItem.Click
If TreeView1.SelectedNode Is Nothing Then
MsgBox("Select A Node")
Else
TreeView1.SelectedNode.Expand()
End If
End Sub
Private Sub CollpaseAllNodesToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CollpaseAllNodesToolStripMenuItem.Click
TreeView1.CollapseAll()
End Sub
Private Sub CollpaseSelectedNodesToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CollpaseSelectedNodesToolStripMenuItem.Click
If TreeView1.SelectedNode Is Nothing Then
MsgBox("Select A Node")
Else
TreeView1.SelectedNode.Collapse()
End If
End Sub
Private Sub ScanneAllNodesToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ScanneAllNodesToolStripMenuItem.Click
For Each tnode In TreeView1.Nodes
Call scann_node(tnode)
Next
End Sub
Sub scann_node(ByVal tnode As TreeNode)
Static indentlevel As Integer
Application.DoEvents()
ListBox1.Items.Add(Space(indentlevel) & tnode.Text)
If tnode.Nodes.Count > 0 Then
indentlevel += 5
For Each tnode In tnode.Nodes
scann_node(tnode)
Next
indentlevel -= 5
End If
End Sub
End Class
Comments
Post a Comment