.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Sandeep Singh

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets


Posted By:Athira Sreejith       Posted Date: January 02, 2009    Points: 10    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  

The tree view control is used to display the hierarchy of nodes (both parent nodes and child nodes). Each element in a tree View is called as Nodes. The top most element of the tree is called as the Root Node. We can expand and collapse these nodes by clicking them. This control is similar to Windows Explorer which
displays a tree view in it's left pane to list all the folders on the hard disk. Name the Tree View named as FoldersTreeView and set it's dock property to Fill. Initially we are declaring all the drives and nodes of the Tree View. Then in the Explorer form load event First declare drives then we pick the drives from the environment. Then here we are creating a new node, it is assigned and hence viewed by the Tree View. And finally we are selecting the image for the drives.



Private FolderNode As TreeNode
Private CurrentFolderNode As TreeNode
Private Drives() As String
The last two lines of the above coding will already been included in the ListView Panel and we would have given the path as "C:\WINDOWS" but here we are giving the path as "C:\".
' Creating a New Node
Private Sub ExplorerForm_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Drives = System.Environment.GetLogicalDrives()
ienm = Drives.GetEnumerator()
While ienm.MoveNext
FolderNode = New TreeNode
FolderNode.Text = ienm.Current.ToString
FolderNode.ImageIndex = 0
FolderNode.SelectedImageIndex = 2
End While
CurrentDirectory = New System.IO.DirectoryInfo("C:\")
End Sub

Inorder to create foldernode first we are getting the items in the Current Directory. Then we have add the nodes from the Current node. Because all the nodes which we are adding are child nodes. And hence we are adding image to the Folder.

Creating Child Node

Private Sub FoldersTreeView_MouseDown(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles FoldersTreeView.MouseDown
If (e.Button.Equals(Windows.Forms.MouseButtons.Left)) Then
CurrentFolderNode = FoldersTreeView.GetNodeAt(New Point(e.X, e.Y))
If CurrentFolderNode Is Nothing Then Return
CurrentDirectory = New System.IO.DirectoryInfo(CurrentFolderNode.FullPath)
Folders = CurrentDirectory.GetDirectories()
ienm = Folders.GetEnumerator
While ienm.MoveNext
Folder = ienm.Current
FolderNode = New TreeNode
FolderNode.Text = Folder.Name
FolderNode.ImageIndex = 4
FolderNode.SelectedImageIndex = 9
End While
End If
End Sub

Mouse Up Event

Private Sub FoldersTreeView_MouseUP(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles FoldersTreeView.MouseDown

If e.Button.Equals(Windows.Forms.MouseButtons.Right) Then
CurrentFolderNode = FoldersTreeView.GetNodeAt(New Point(e.X, e.Y))
If CurrentFolderNode Is Nothing Then Return
FoldersContextMenuStrip.Show(FoldersTreeView, e.X, e.Y)
End If
End Sub


The output of the above Program is given below, Here C:\ is the Root Node and following that we have set of Files and Folders in that directory.


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend