ARTICLE

Directory Picker in VB.NET

Posted by Mike Gold Articles | Visual Basic 2010 September 05, 2003
This article illustrates the use of a treeview in a directory tree user control. You can use this control to visually select files or directories from any file structure on your computer system.
Download Files:
 
Reader Level:

This article illustrates the use of a treeview in a directory tree user control.  You can use this control to visually select files or directories from any file structure on your computer system.

The treeview uses these node structures for handling most of its data population.  The images from the tree view come from an ImageList component.  The ImageList  must be constructed seperately, and the icons used in the imagelist are added to the Images Collection from pictures created in an image editor (like mspaint or something):

The ImageList is then associated to the TreeView via the ImageList property:

When nodes are constructed, they are assigned indexes that point to a particular image in the list.

Now let's take a good look at our directory tree user control.  The code for populating the directory nodes takes advantage of the Directory class in the System.IO namespace.  A tree is built by first constructing the node with the name, image index, and image index when a node is selected.  Then the node is added to the parent node.

Protected Sub PopulateNode(ByVal aNode As TreeNode)
Me.Cursor = Cursors.WaitCursor ' Set up the wait cursor
Dim strDir As String = BuildDirectory(aNode) ' Build the directory tree string from the tree node
Dim Dir As New Directory(strDir) ' Use the Directory class to get the files and directory names in the directory
Dim count As Integer = 0
' Get each directory name and add it as a directory folder tree node
Dim i As Integer
For i = 0 To (Dir.GetDirectories().Length) - 1
Dim ChildNode As New TreeNode(Dir.GetDirectories()(i).Name, 1, 0)aNode.Nodes.Add(i, ChildNode)
count += 1
Next i
' Get each file name and add it as a file tree node
Dim i As Integer
For i = 0 To (Dir.GetFiles(m_strFilter).Length) - 1
Dim ChildNode As New TreeNode(Dir.GetFiles(m_strFilter)(i).Name, 2, 2)aNode.Nodes.Add(i + count, ChildNode)
Next i
aNode.Expand()
' show the children nodes
Me.Cursor = Cursors.Arrow ' restore the cursor
End Sub 'PopulateNode

The function BuildDirectoryTree is used to construct the string needed to go to the file directory being populated and is shown below:

Protected Function BuildDirectory(ByVal aNode As TreeNode) As String
Dim theNode As TreeNode = aNode
Dim strDir As String = ""
' Cycle through the node and all its parents to build the full path of the directory
While Not (theNode Is Nothing)
If theNode.Text((theNode.Text.Length - 1)) <> "\"c Then
strDir = "\" + strDir
End If
strDir = theNode.Text + strDir
theNode = theNode.Parent
End While
Return strDir
End Function 'BuildDirectory

The event AfterSelect on a TreeView, can be used to trap the selection event of a particular node. In the case of the directory tree component, we use this selection event  as an opportunity to populate an empty node:

Public Sub treeView1_AfterSelect(ByVal sender As Object, ByVal e As System.WinForms.TreeViewEventArgs)
' check to see if the selected node is already populated
If e.node.Nodes.Count > 0 Or e.node.ImageIndex = 2 Then
Return
End If
' The node is not populated, so populate the selected node
PopulateNode(e.node)
End Sub 'treeView1_AfterSelect

This control can be used as a starting point for creating explorer like applications.  The Path and FileName properties serve as outputs of the control for obtaining directory information selected by the user.  The following code illustrates how the information can be obtained by the user after a button is pressed.

Protected Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
label3.Text = dirTree1.Path
label4.Text = dirTree1.FileName
End Sub 'button1_Click

In future articles we will explore the ability of trapping events directly from the control rather than from a button press.  Please write or post comments in the discussion forum if you have any ideas on expanding the capability of this control.

share this article :
post comment
 

unfortunately the code is C#, not VB in the download

Posted by Mike Oct 17, 2008
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor