The FolderBrowserDialog Control - Visual Basic 2008
Sometimes we need to prompt users for a folder, rather than a filename. An application that processes files in batch mode shouldn't force users to select the files to be processed. Instead, it should allow users to select a folder and process all files of a specific type in the folder (it could encrypt all text documents or resize all image files, for example). As elaborate as the File Open dialog box might be, it doesn't allow the selection of a folder. To prompt users for a folder's path, use the FolderBrowser dialog box, which is a very simple one; it's shown in Figure 4.15 in "Folder Browsing Example Project." listed below. The FolderBrowserDialog control exposes a small number of properties, which are discussed next.
This property indicates the initial folder to be displayed when the dialog box is shown. It is not necessarily a string; it can also be a member of the SpecialFolder enumeration. To see the members of the enumeration, enter the following expression:
As soon as you enter the equals sign, you will see the members of the enumeration. The most common setting for this property is My Computer, which represents the target computer's file system. You can set the RootFolder property to a number of special folders (for example, Personal, Desktop, ApplicationData, LocalApplicationData, and so on). You can also set this property to a string with the desired folder's pathname.
After the user closes the FolderBrowser dialog box by clicking the OK button, you can retrieve the name of the selected folder with the SelectedFolder property, which is a string, and you can use it with the methods of the System.IO namespace to access and manipulate the selected folder's files and subfolders.
This property determines whether the dialog box will contain a New button; its default value is True. When users click the New button to create a new folder, the dialog box prompts them for the new folder's name, and creates a new folder with the specified name under the selected folder.
Folder Browsing Example Project - VB.NET 2008
The FolderBrowser control is a trivial control, but I'm including a sample application to demonstrate its use. The same application demonstrates how to retrieve the files and subfolders of the selected folder and how to create a directory listing in a RichTextBox control, like the one shown in Figure 4.15. The members of the System.IO namespace, which allow you to access and manipulate files and folders from within your code, are discussed in detail in Chapter "Accessing Folders and Files".
The FolderBrowser dialog box is set to display the entire file system of the target computer and is invoked with the following statements:
As usual, we examine the value returned by the ShowDialog method of the control and we proceed if the user has closed the dialog box by clicking the OK button. The code that iterates through the selected folder's files and subfolders, shown in Listing 4.18, is basically a demonstration of some members of the System.IO namespace, but I'll review it briefly here.
Listing 4.18: Scanning a Folder
The selected folder's name is stored in the initialFolder variable and is passed as an argument to the constructor of the DirectoryInfo class. The InitialDir variable represents the specified folder. This object is passed to the PrintFolderName() subroutine, which prints the folder's name in bold. Then the code iterates through the same folder's files and prints them with the PrintFileNames() subroutine, which accepts as an argument the DirectoryInfo object that represents the current folder and the indentation level. After printing the initial folder's name and the names of the files in the folder, the code iterates through the subfolders of the initial folder. The GetDirectories method of the DirectoryInfo class returns a collection of objects, one for each subfolder under the folder represented by the InitialDir variable. For each subfolder, it calls the PrintDirectory() subroutine, which prints the folder's name and the files in this folder, and then iterates through the folder's subfolders. The code that iterates through the selected folder's files and subfolders is shown in Listing 4.19.
Listing 4.19: The PrintDirectory() Subroutine
The code that iterates through the subfolders of a given folder is discussed in detail in Chapter "Accessing Folders and Files", so you need notworry if you can't figure out how it works yet. In the next section, you'll learn how to display formatted text in the RichTextBox control.
More on Common Dialog Controls
Table of Contents
W3computing.com Copyright 2011 © All Rights Reserved