Reusing Controls: Working with Built-in Dialog Boxes

In this lesson, you will learn how to use some of the built-in dialog boxes that are available in Visual Basic Express. Instead of creating your own dialog box to enable users to select a color for the background of a form, or creating your own Open File dialog box, you can use these built-in dialog boxes. This ensures that your programs have the same appearance and behavior as other Windows-based programs.

There are several dialog box components available for use in your applications. You can find them on the Dialogs tab of the Toolbox. When you add these types of components to your application, you cannot see them as controls on the form. Instead, they are added to the component tray, underneath the form. You can learn more about components in the lesson Invisible Controls: Using Components.

You can let users navigate to a folder by using the FolderBrowserDialog component. For example, instead of displaying the names of graphic files in the Pictures folder, as described in the lesson Retrieving the Names of Files in a Folder, you could use the FolderBrowserDialog component to let users navigate to any folder that holds graphic files.

To display a dialog box, you use the ShowDialog method. You can then check whether the user clicked the OK button by using the DialogResult.OK field.

Try It!

To display the folder browser dialog box

  1. On the File menu, click New Project.

    The New Project dialog box appears.

  2. Click Windows Forms Application and then click OK.

  3. Add a Label to the form, leaving the default name, Label1.

  4. Add a Button control to the form, and change the following properties in the Properties window:

    Property

    Value

    Name

    FolderPath

    Text

    Path

  5. Add a FolderBrowserDialog component to the form.

    FolderBrowserDialog1 appears in the component tray.

  6. Double-click the button to enter the default event handler in the Code Editor.

  7. In the FolderPath_Click event handler, add the following code to display the folder browser dialog box and display the selected path in the label.

    If FolderBrowserDialog1.ShowDialog() = 
         Windows.Forms.DialogResult.OK Then
    
         Label1.Text = FolderBrowserDialog1.SelectedPath
    
    End If
    
  8. Press F5 to run the code.

  9. When the form appears, click Path, click a folder in the list, and then click OK.

  10. Verify that the selected path appears in the label.

  11. Close the application.

Applying a Font to Text

You can use the FontDialog component to enable users to select from a list of fonts. By default, the ability to select a color for the font is not available on the dialog box. You can enable this capability by setting the ShowColor property.

To display the font dialog box

  1. Add another Button control to the form, and change the following properties in the Properties window:

    Property

    Value

    Name

    TextFont

    Text

    Font

  2. Add a FontDialog component to the form.

    FontDialog1 appears in the component tray.

  3. Double-click the Font button to enter the default event handler in the Code Editor.

  4. In the TextFont_Click event handler, add the following code to display the font dialog box and apply the user's font changes to the text in the label.

    FontDialog1.ShowColor = True
    If FontDialog1.ShowDialog() = 
        Windows.Forms.DialogResult.OK Then
        Label1.Font = FontDialog1.Font
        Label1.ForeColor = FontDialog1.Color
    End If
    
  5. Press F5 to run the code.

  6. When the form opens, click Path, click a folder in the list, and then click OK.

  7. Click Font, choose a font and color in the dialog box, and then click OK.

  8. Verify that the selected font and color is applied to the text in the label.

  9. Close the application.

Applying a Color to the Form

If you want to enable users to select a color without applying it to a font, you can use the ColorDialog component. For example, you can apply a chosen color to the BackColor property of the label.

To display the color dialog box

  1. Add another Button control to the form, and change the following properties in the Properties window:

    Property

    Value

    Name

    FormColor

    Text

    Color

  2. Add a ColorDialog component to the form.

    ColorDialog1 appears in the component tray.

  3. Double-click the Color button to enter the default event handler in the Code Editor.

  4. In the FormColor_Click event handler, add the following code to display the color dialog box and change the background color of the form according to the user's choice.

    If ColorDialog1.ShowDialog() = 
        Windows.Forms.DialogResult.OK Then
        Me.BackColor = ColorDialog1.Color
    End If
    
  5. Press F5 to run the code.

  6. When the form opens, click Color, click a color in the resulting dialog box, and then click OK.

  7. Verify that the chosen color is applied to the form.

  8. Close the application.

Next Steps

In this lesson, you learned how to use the built-in functionality of the FolderBrowserDialog component, the FontDialog component, and the ColorDialog component in your applications.

In the next lesson, you'll learn how to create standard toolbars for an application.

Next Lesson: Pushing Buttons: Adding Toolbars and Buttons

See Also

Reference

FolderBrowserDialog Component Overview (Windows Forms)

FontDialog Component Overview (Windows Forms)

ColorDialog Component Overview (Windows Forms)

Other Resources

Creating the Visual Look of Your Program: Introduction to Windows Forms