FolderBrowserDialog


The FolderBrowserDialog component displays a dialog box that lets the user select a folder (directory) in the file system. The program displays the dialog box by calling the component’s ShowDialog method.

The following code shows how a program can use the dialog box. The program sets the dialog component’s Description property to a string telling the user to select the folder containing configuration data and calls the component’s ShowDialog method. If the user selects a folder and clicks OK, ShowDialog returns DialogResult.OK, and the program calls the LoadConfiguration subroutine, passing it the name of the folder that the user selected.

  dlgFolder.Description = _     "Select the folder that contains the configuration data XML files." If dlgFolder.ShowDialog() = DialogResult.OK Then     LoadConfiguration(dlgFolder.SelectedPath) End If 

Figure G-6 shows the dialog box. Notice that the Description text is displayed above the folder area.

image from book
Figure G-6: The FolderBrowserDialog component lets the user select a directory.

The component’s SelectedPath property returns the path selected by the user, but it also determines where the dialog begins browsing. If your code uses the dialog box, does not change this value, and then uses the dialog box again later, the dialog box starts the second time where it left off the first time.

The program can also explicitly set the SelectedPath value to start browsing at a particular folder. For example, the following code makes the dialog begin browsing in the C:\Temp directory:

  dlgFolder.SelectedPath = "C:\Temp" 

Alternatively, you can use the component’s RootFolder property to make the component start in one of the system folders. Set this property to one of the Environment.SpecialFolder values shown in the following list. For example, to start browsing in the MyPictures directory, set RootFolder to Environment

 ApplicationData                 InternetCache                Programs CommonApplicationData           LocalApplicationData         Recent CommonProgramFiles              MyComputer                   SendTo Cookies                         MyMusic                      StartMenu Desktop                         MyPictures                   Startup DesktopDirectory                Personal                     System Favorites                       ProgramFiles                 Templates History

The dialog box will not allow the user to leave the root folder. For example, if you set the RootFolder property to Environment.SpecialFolder.ProgramFiles, then the user will be able to browse through the Program Files hierarchy (normally, C:\Program Files), but will not be able to move to other parts of the system.

If you want to start browsing at a particular directory but want to allow the user to move to other parts of the directory hierarchy, leave the RootFolder with its default value of Environment.SpecialFolder .Desktop and then set the SelectedPath property appropriately. For example, the following code uses the Environment object’s SpecialFolder method to make the browser start browsing at the Program Files folder:

  dlgFolder.RootFolder = Environment.SpecialFolder.Desktop dlgFolder.SelectedPath = _     Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) 




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net