View files in a TreeView
Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
4 posts
Page 1 of 1
This tutorial will show you how to view files in a TreeView. You do not need to add anything but a TreeView to your form, but remember to import System.IO in your code.
In your Form_Load event, we will load the logical drives on the computer so we can browse through the files.
Then it sets the Tag property of the TreeNode to the root directory of the drive. This way we can quickly see the contents later.
In the end we add the new TreeNode to our TreeView.
Whenever we expand a node, the event BeforeExpand is raised. In this event, we will put our code to load directories and files into the TreeView.
If the directory has not previously been loaded, remove the "*" node and get the store the directory of the node in a variable we call 'directory'.
Next up is to load the directories
If something went wrong we display the error.
Now we need to loop through our files in the directory
Your full code should now look like this
In your Form_Load event, we will load the logical drives on the computer so we can browse through the files.
Code: Select all
This loops through each drive that it can access, and checks if it is ready to use before continuing. A new TreeNode is created with the name of the drive. If it also has a label like "System" or "Packard Bell" it will be added to the text of the node. For Each Drive As DriveInfo In DriveInfo.GetDrives()
If Drive.IsReady Then
Dim node As New TreeNode(Drive.Name)
If Not String.IsNullOrEmpty(Drive.VolumeLabel) Then node.Text &= " - " & Drive.VolumeLabel
node.Tag = Drive.RootDirectory
node.Nodes.Add("*")
TreeView1.Nodes.Add(node)
End If
Next
Then it sets the Tag property of the TreeNode to the root directory of the drive. This way we can quickly see the contents later.
In the end we add the new TreeNode to our TreeView.
Whenever we expand a node, the event BeforeExpand is raised. In this event, we will put our code to load directories and files into the TreeView.
Code: Select all
If the the first node is not "*", the directory has either already been loaded, or does not contain any sub items, so return. If Not e.Node.FirstNode.Text = "*" Then Return
e.Node.Nodes.Clear()
Dim directory As DirectoryInfo = CType(e.Node.Tag, DirectoryInfo)
If the directory has not previously been loaded, remove the "*" node and get the store the directory of the node in a variable we call 'directory'.
Next up is to load the directories
Code: Select all
Here we loop through every sub-directory and create a new node, with the Tag set to the directory the node represents. If the directory contains files or other subdirectories, add "*" to mark that. After we have set up our node, add it to the parent node. Try
For Each d As DirectoryInfo In directory.GetDirectories()
Dim node As New TreeNode(d.Name)
node.Tag = d
If d.GetDirectories().Count + d.GetFiles().Count > 0 Then
node.Nodes.Add("*")
End If
e.Node.Nodes.Add(node)
Next
Catch ex As Exception
MessageBox.Show("Error - " & ex.GetType().FullName & vbNewLine & ex.Message)
End Try
If something went wrong we display the error.
Now we need to loop through our files in the directory
Code: Select all
This is a bit easier code because a file cannot contain subdirectories or files. Here we only have to create a node with the name of the file and add it to the TreeView. If an error occurred, display it. Try
For Each d As FileInfo In directory.GetFiles()
Dim node As New TreeNode(d.Name)
e.Node.Nodes.Add(node)
Next
Catch ex As Exception
MessageBox.Show("Error - " & ex.GetType().FullName & vbNewLine & ex.Message)
End Try
Your full code should now look like this
Code: Select all
Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
For Each Drive As DriveInfo In DriveInfo.GetDrives()
If Drive.IsReady Then
Dim node As New TreeNode(Drive.Name)
If Not String.IsNullOrEmpty(Drive.VolumeLabel) Then node.Text &= " - " & Drive.VolumeLabel
node.Tag = Drive.RootDirectory
node.Nodes.Add("*")
TreeView1.Nodes.Add(node)
End If
Next
End Sub
Private Sub TreeView1_BeforeExpand(sender As System.Object, e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand
If Not e.Node.FirstNode.Text = "*" Then Return
e.Node.Nodes.Clear()
Dim directory As DirectoryInfo = CType(e.Node.Tag, DirectoryInfo)
Try
For Each d As DirectoryInfo In directory.GetDirectories()
Dim node As New TreeNode(d.Name)
node.Tag = d
If d.GetDirectories().Count + d.GetFiles().Count > 0 Then
node.Nodes.Add("*")
End If
e.Node.Nodes.Add(node)
Next
Catch ex As Exception
MessageBox.Show("Error - " & ex.GetType().FullName & vbNewLine & ex.Message)
End Try
Try
For Each d As FileInfo In directory.GetFiles()
Dim node As New TreeNode(d.Name)
e.Node.Nodes.Add(node)
Next
Catch ex As Exception
MessageBox.Show("Error - " & ex.GetType().FullName & vbNewLine & ex.Message)
End Try
End Sub
End Class
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
Nice tutorial MrAksel!
Keep it up! cooll;
Keep it up! cooll;
Instead of LOL use this -
LSIBMHBIWFETALOL
Which means -
Laughing silently in between my head because it wasn't funny enough to actually laugh out loud!
LSIBMHBIWFETALOL
Which means -
Laughing silently in between my head because it wasn't funny enough to actually laugh out loud!
4 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023