What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleUse standard dialogs in Visual Basic .NET
DescriptionThis example shows how to use standard dialogs in Visual Basic .NET.
Keywordsdialog, standard dialog, FontDialog, OpenFileDialog, SaveFileDialog, ColorDialog, use standard dialogs, Visual Basic, Visual Basic .NET, VB, VB.NET
CategoriesSoftware Engineering, Controls
To use a standard dialog, first place the dialog on a form and set any properties that you want to customize. For example, you may want to set a file dialog's Filter property. You may also want to set CheckFileExists for the OpenFileDialog and OverwritePrompt or CreatePrompt for the SaveFileDialog. (Although the defaults for those properties are sensible.)

You can create the dialogs in code at run time instead of placing them on a form but it's more convenient to use a form.

Using a dialog at run time is a three step process.

  1. Initialize the dialog. Set dialog properties to show the current settings. For example, set a FontDialog's Font property to the current font.
  2. Display the dialog and check the return result. Call the dialog's ShowDialog method and see if it returns OK, indicating that the user clicked OK.
  3. If the user clicked OK, use the dialog's values to do something. For example, make the form use the font selected in a FontDialog.

The example program lets the user select a file for opening, a file for saving, foreground and background colors, and a font. The following code shows how the program handles font selection.

' Select a font.
Private Sub btnFont_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnFont.Click
    ' Initialize.
    fdFont.Font = Me.Font

    ' Display and check result.
    If (fdFont.ShowDialog() = DialogResult.OK) Then
        ' Take action.
        Me.Font = fdFont.Font
    End If
End Sub
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.