Using Dialogs




Dialogs in VB.Net are application modal Forms. That means that while they are open, you can't navigate back to the parent Form or any other Form in your Project, without first closing the Dialog. Also the execution of code on the calling thread is suspended until you close the Dialog.



Below is an example program that demonstrates how to use the Common Dialogs + also your own Custom Dialogs.


(Click on the buttons to navigate)





This is the code for the OpenFileDialog:





Back to top



    Private Sub btnOpenFileDialog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenFileDialog.Click

        'declare new OpenFileDialog + set it's initial properties
        Dim ofd As New OpenFileDialog With { _
        .Title = "Select image file" & If(chkMultiSelect.Checked, "s", ""), _
        .Filter = "BMP (*.bmp)|*.bmp|GIF (*.gif)|*.gif|JPG (*.jpg)|*.jpg|PNG (*.png)|*.png|All Files (*.*)|*.*", _
        .FilterIndex = 0, _
        .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), _
        .Multiselect = chkMultiSelect.Checked}

        'show the dialog + display the results in a msgbox unless cancelled
        If ofd.ShowDialog = DialogResult.OK Then
            MessageBox.Show(String.Format("You chose: {0}{0}{1}", Environment.NewLine, _
                            If(chkMultiSelect.Checked, String.Join(Environment.NewLine, ofd.FileNames), ofd.FileName)))
        End If

    End Sub




This is the code for the FolderBrowserDialog:





Back to top



Private Sub btnFolderBrowserDialog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFolderBrowserDialog.Click

    'declare new FolderBrowserDialog + set it's initial properties
    Dim fbd As New FolderBrowserDialog With { _
    .Description = "Select a folder", _
    .ShowNewFolderButton = True, _
    .SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)}

    'show the dialog + display the result in a msgbox unless cancelled
    If fbd.ShowDialog = DialogResult.OK Then
        MessageBox.Show(String.Format("You chose: {0}{0}{1}", Environment.NewLine, fbd.SelectedPath))
    End If

End Sub     




This is the code for the SaveFileDialog:





Back to top



Private Sub btnSaveFileDialog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveFileDialog.Click

    'declare new SaveFileDialog + set it's initial properties
    Dim sfd As New SaveFileDialog With { _
    .Title = "Choose file to save to", _
    .Filter = "BMP (*.bmp)|*.bmp|GIF (*.gif)|*.gif|JPG (*.jpg)|*.jpg|PNG (*.png)|*.png|All Files (*.*)|*.*", _
    .FilterIndex = 0, _
    .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)}

    'show the dialog + display the results in a msgbox unless cancelled
    If sfd.ShowDialog = DialogResult.OK Then
        MessageBox.Show(String.Format("You chose to save to: {0}{0}{1}", Environment.NewLine, sfd.FileName))
    End If

End Sub       




This is the code for the FontDialog:





Back to top



Private Sub btnFontDialog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFontDialog.Click

    'declare new FontDialog + set it's initial properties
    Dim fd As New FontDialog With { _
    .Font = lblNote.Font}

    'show the dialog + set lblNote.Font unless cancelled
    If fd.ShowDialog = DialogResult.OK Then
        lblNote.Font = fd.Font
    End If

End Sub          




This is the code for the ColorDialog:





Back to top



Private Sub btnColorDialog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnColorDialog.Click

    'declare new ColorDialog + set it's initial properties
    Dim cd As New ColorDialog With { _
    .AllowFullOpen = True, _
    .Color = lblNote.ForeColor}

    'show the dialog + set lblNote.ForeColor unless cancelled
    If cd.ShowDialog = DialogResult.OK Then
        lblNote.ForeColor = cd.Color
    End If

End Sub           




This is the code for the MessageBox:





Back to top



    Private Sub btnMessageBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMessageBox.Click

        'to use a messagebox
        Dim response As DialogResult = MessageBox.Show("Visually enumerate messagebox types?", _
                                      Me.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
        If response = DialogResult.Yes Then
            'this visually enumerates the messagebox types
            Dim buttons() As MessageBoxButtons = DirectCast([Enum].GetValues(GetType(MessageBoxButtons)), MessageBoxButtons())
            Dim icons() As MessageBoxIcon = DirectCast([Enum].GetValues(GetType(MessageBoxIcon)), MessageBoxIcon())
            Dim defaultButton() As MessageBoxDefaultButton = DirectCast([Enum].GetValues(GetType(MessageBoxDefaultButton)),  _
                                                                                                    MessageBoxDefaultButton())

            For x As Integer = 0 To 8
                MessageBox.Show(String.Format("MessageBoxIcon: {0}", icons(x).ToString), _
                                Me.Text, MessageBoxButtons.OK, icons(x))
            Next

            Dim buttonIndex As Integer = 3

            For x As Integer = 0 To 5
                If x >= 2 And x <= 4 Then buttonIndex -= 1 Else buttonIndex = 3
                MessageBox.Show(String.Format("MessageBoxButtons: {0}{1}MessageBoxDefaultButton: {2}", _
                                              buttons(x).ToString, Environment.NewLine, _
                                              If(buttonIndex < 3, defaultButton(buttonIndex).ToString, defaultButton(0).ToString)), _
                                              Me.Text, buttons(x), MessageBoxIcon.None, If(buttonIndex < 3, _
                                                                                           defaultButton(buttonIndex), defaultButton(0)))
            Next

        End If

    End Sub




This is the code for the InputBox:





Back to top



Private Sub btnInputBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInputBox.Click

    'ask for user's name
    Dim name As String = InputBox("Enter your name...", Me.Text, "NoName")
    If name <> "" Then MessageBox.Show("Hello " & name)

End Sub    




This is the code for the PageSetupDialog:





Back to top



Private Sub btnPageSetupDialog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPageSetupDialog.Click

    'declare new PageSetupDialog + set it's initial properties
    Dim psd As New PageSetupDialog With { _
    .AllowMargins = True, _
    .AllowOrientation = True, _
    .AllowPaper = True, _
    .Document = PrintDocument1, _
    .PageSettings = PrintDocument1.DefaultPageSettings}

    'show the dialog, set PrintDocument1.DefaultPageSettings + display the PrintPreviewDialog unless cancelled
    If psd.ShowDialog = DialogResult.OK Then
        PrintDocument1.DefaultPageSettings = psd.PageSettings
        btnPrintPreviewDialog.PerformClick()
    End If

End Sub      




This is the code for the PrintDialog:





(Note: The PrintDialog will only work in an x86 Application)



Back to top



Private Sub btnPrintDialog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintDialog.Click

        'declare new PrintDialog + set it's initial properties
        Dim pd As New PrintDialog With { _
        .Document = PrintDocument1, _
        .PrinterSettings = PrintDocument1.PrinterSettings}

        'show the dialog + set PrintDocument1.PrinterSettings unless cancelled
        'then offer to print test page
        If pd.ShowDialog = DialogResult.OK Then
            PrintDocument1.PrinterSettings = pd.PrinterSettings
            Dim response As DialogResult = MessageBox.Show("Print test page?", Me.Text, MessageBoxButtons.YesNo, _
                                                           MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
            If response = DialogResult.Yes Then
                PrintDocument1.Print()
            End If
        End If

    End Sub




This is the code for the PrintPreviewDialog:





Back to top



Private Sub btnPrintPreviewDialog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintPreviewDialog.Click

    'declare new PrintPreviewDialog + set it's initial properties
    Dim ppd As New PrintPreviewDialog With { _
    .Document = PrintDocument1, _
    .windowstate = FormWindowState.Maximized}

    ppd.PrintPreviewControl.AutoZoom = False
    ppd.PrintPreviewControl.Zoom = 1.0

    'no need to test return value with this 1
    ppd.ShowDialog()

End Sub      




This is the code for the CustomDialog:



Back to top



Private Sub btnCustomDialog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCustomDialog.Click

    'declare new frmCustomDialog
    Dim frm As New frmCustomDialog

    'show the dialog + display the dialog's userText property in a msgbox unless cancelled
    If frm.ShowDialog = DialogResult.OK Then
        MessageBox.Show(frm.userText)
    End If

End Sub         




This is the CustomDialog form:



Back to top






An important point when creating Custom Dialogs, is to set your command Button's DialogResult Property.
In the example Project, btnOK's DialogResult = DialogResult.OK + btnCancel's DialogResult = DialogResult.Cancel.



Back to top



It contains this code:



Public Class frmCustomDialog

    Private _userText As String

    Public Property userText() As String
        Get
            Return _userText
        End Get
        Set(ByVal value As String)
            _userText = value
        End Set
    End Property

    Private Sub txtUserText_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtUserText.TextChanged
        userText = txtUserText.Text
    End Sub

End Class        




You can download the example project here: Dialogs.zip