Browse DevX
Sign up for e-mail newsletters from DevX


Build a Souped Up File Explorer for Your PDA : Page 2

Replace the lackluster File Explorer that comes with the Pocket PC with one that better mimics Windows Explorer and learn to manipulate the .NET Compact Framework's TreeView control in the exchange.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Coding the Controls
First, import the System.IO namespace. This namespace is required for methods that I will use later to perform directory operations:

Imports System.IO

Next, define the three constants for referring to the three images in the ImageList control:

' Constants for the icon images Const icoOpen = 0 Const icoClose = 1 Const icoFile = 2

And the variables required:

Dim copy As Boolean = True ' whether it is a copy operation Dim fileName As String ' filename of file to be copied or cut Dim pathName As String ' full pathname of file to be copied or cut

When the form is loaded, display a node representing the root directory:

Private Sub Form1_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load Dim node As New TreeNode Try ' ---create the root node--- node.ImageIndex = icoClose node.SelectedImageIndex = icoOpen node.Text = "\" TreeView1.Nodes.Add(node) ' ---Add the dummy node--- node.Nodes.Add("") Catch err As Exception MsgBox(err.ToString) End Try End Sub

Note that I add a dummy node after each node is created. This is to ensure that the current node can be expanded to reveal subdirectories (even if there are none). This is shown in Figure 4.

Figure 4. Dummy Node: Adding a dummy node to ensure that the directory can be expanded.
When a node is expanded (by clicking on the "+" symbol), the TreeView1_BeforeExpand event is called. You have to write code that checks to see if the current node is a leaf node (meaning it is not a directory, but a file). If it is a leaf node, exit the method. Otherwise you need to display its sub-directories (if any).

You should also change the current node icon to "open" if the node is selected and "closed" if the node is not selected Here is the code for expanding folders and displaying the proper icon at each node:

Private Sub TreeView1_BeforeExpand _ (ByVal sender As Object, _ ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) _ Handles TreeView1.BeforeExpand ' ---if leaf node then exit--- If e.Node.ImageIndex = icoFile Then Return ' ---remove the dummy node and display ' the subdirectories and files--- Try e.Node.Nodes.Clear() ' clears all the nodes and... displayChildNodes(e.Node) ' create the nodes again Catch err As Exception MsgBox(err.ToString) End Try ' ---change the icon for this node to open--- If e.Node.GetNodeCount(False) > 0 Then e.Node.ImageIndex = icoClose e.Node.SelectedImageIndex = icoOpen End If End Sub

The displayChildNodes() method displays all the files and subdirectories within the current directory in the TreeView control:

Private Sub displayChildNodes _ (ByVal parentNode As System.Windows.Forms.TreeNode) Dim FS As New DirectoryInfo(stripExtraSlash _ (parentNode.FullPath)) Dim dirInfo As DirectoryInfo Dim fileInfo As FileInfo Try ' ---displays all dirs--- For Each dirInfo In FS.GetDirectories() ' ---create a new node to be added--- Dim node As New TreeNode node.Text = dirInfo.Name ' name of file or dir node.ImageIndex = icoClose node.SelectedImageIndex = icoOpen parentNode.Nodes.Add(node) ' ---add the dummy node--- node.Nodes.Add("") Next Catch err As Exception MsgBox(err.ToString) End Try Try ' --display all files--- For Each fileInfo In FS.GetFiles() ' ---create a new node to be added--- Dim node As New TreeNode Node.Text = fileInfo.Name Node.ImageIndex = icoFile Node.SelectedImageIndex = icoFile parentNode.Nodes.Add(Node) Next Catch err As Exception MsgBox(err.ToString) End Try End Sub

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date