The ColorDialog Control
In this Exercise, you continue using the same Dialogs project and have the FolderBrowserDialog control display the Browse For Folder dialog box. Then, if the dialog box returns a DialogResult of OK, you’ll display the selected folder in the text box on your form.
Exercise - Working with the ColorDialog Control
1. Switch to the Forms Designer in the Dialogs project.
2. On the form, add another Button control from the Toolbox and set its properties according to the values shown:
-
Set Name to btnBrowse.
-
Set Anchor to Top, Right.
-
Set Location to 367, 158.
-
Set Text to Browse.
3. Next, add a FolderBrowserDialog control to your project from the Toolbox. It will be added to the workspace below the form. Accept all default properties for this control, because you’ll set the necessary properties in your code.
4. Double-click the Browse button to bring up its Click event procedure, and add the following code:
Private Sub btnBrowse_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnBrowse.Click
‘Set the FolderBrowserDialog control properties
FolderBrowserDialog1.Description = “Select a folder for your backups:”
FolderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer
FolderBrowserDialog1.ShowNewFolderButton = False
‘Show the Browse For Folder dialog
If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
‘Display the selected folder
txtFile.Text = FolderBrowserDialog1.SelectedPath
End If
End Sub |
5. That’s all the code you need to add. To test your changes to this project, click the Start button.
6. When your form displays, click the Browse button, and you’ll see a Browse For Folder dialog similar to this:
7. Now browse your computer and select a folder. When you click the OK button, the selected folder will be displayed in the text box on your form as shown in below Figure. Notice that the folder returned contains a fully qualified path name.
click next Page - How It Works. |