Dialog Boxes in VB.net – Color, Font, OpenFile and Print

What are Dialog Boxes in VB.net?

Dialog Boxes in VB.net is a temporary window for an application that lets you open or save a file, get notifications, alert messages, change the color, print, open a file, etc., by using the mouse or the keyboard.

It is also helpful to create ways for the user and the application to talk to each other and work together.

Also, the dialog box shows up in a form when the program needs to talk to the user, such as when there is an error, an alert message, or an acknowledgment from the user.

Or when the program needs the user to do something right away or decide if the changes should be saved.

All VB.NET Dialog Boxes inherit from the CommonDialog class and override the RunDialog() method of the base class to make the OpenFileDialog Box, PrintDialog Box, Color Dialog Box, and Font Dialog Box.

When a dialog box calls a Windows form’s ShowDialog() method, the RunDialog() method is automatically called.

We have learned how to use the ListView Control in VB.net in the previous lesson.

In this lesson, we shall learn How To Write a Program for Dialog Boxes.

ShowDialog() Functions in VB.net

The ShowDialog() method has the following functions that can be called while the Windows Form is running.

1. Abort

Abort returns DialogResult.Abort value, when the user clicks an Abort button.

2. Cancel

Cancel returns DialogResult.Cancel, when the user clicks a Cancel button.

3. Ignore

Ignore returns DialogResult.Ignore, when the user clicks an Ignore button.

4. No

No returns DialogResult.No, when the user clicks a No button.

5. None

None returns nothing and the dialog box continues running.

6. OK

OK returns DialogResult.OK, when the user clicks an OK button.

7. Retry

Retry returns DialogResult.Retry, when the user clicks a Retry button.

8. Yes

Yes returns DialogResult.Yes, when the user clicks a Yes button

The following diagram shows the common dialog class inheritance:

Common Dialogue Class in VB.net
Common Dialog Class

All of these classes have controls that can be added from the Toolbox during the design process.

You can add the relevant functionality of these classes to your application either by instantiating the class programmatically or by using the relevant controls.

When you double-click on a dialog control in the toolbox or drag it onto the form, it shows up in the Component tray at the bottom of the Windows Forms Designer.

It doesn’t show up directly on the form.

Dialog Boxes Control in VB.net

In the VB.NET Windows Form, there are the Dialog Box Controls that are most often used.

1. Color Dialog Box in VB.net

Color Dialog Box is used to show the color dialog box, which lets the user choose a color from a list of predefined colors or enter their own.

2. Font Dialog Box in VB.net

Font Dialog Box is used to make a Font dialog box, which lets the user choose the font, font size, font color, and font style for the text that is currently selected.

3. OpenFile Dialog Box in VB.net

OpenFile Dialog Box is used to make a box that asks the user to choose a file to open. The user can choose more than one file.

4. Print Dialog Box in VB.net

Print Dialog Box is used to make a print dialog box that lets the user print documents by choosing the printer and page settings in the Windows program.

Let’s make a simple program to show the VB.NET Windows Forms Dialog Box.

Public Class Form1
    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
        Dim result1 As DialogResult = MessageBox.Show("Is programming easy to learn?",
    "Important Question",
    MessageBoxButtons.YesNo)
    End Sub
End Class

Program Output:

Dialogue Box in VB.net Output
Dialog Box Output

Now, when you click on the Windows Form’s “Click Me!” button, the dialog box appears, as shown below.

Dialogue Box in VB.net Display Question Message
Dialog Box Display Question Message

Summary

In this article, we discussed How To Create a Dialog Box in Windows Forms using Microsoft Visual Studio.

After that, we saw the different ShowDialogue Functions and different Dialog Box Controls such as Color Dialog Box, Font Dialog Box, OpenFile Dialog Box, and Print Dialog Box.


Common use cases for Dialog Boxes

Dialog Boxes appears in most Windows Forms applications. Typical scenarios:

  • File save operations. Prompt users to pick a target file name and format before writing data.
  • File open operations. Let users browse for spreadsheets, images, PDFs, or configuration files to import.
  • Font selection. Give users control over typography in rich-text editors, report designers, or accessibility settings.
  • Color selection. Used in drawing apps, chart configurators, and theme editors.
  • Print previews. Route content to the printer with pagination and margins under user control.

Best practices when using Dialog Boxes

  • Always set a default. Users expect a sensible starting value (e.g., the last used color or the current font).
  • Handle DialogResult. Check whether the user clicked OK vs Cancel before applying changes.
  • Set filters correctly. For file dialogs, restrict extensions to the ones your app actually reads.
  • Wrap in Using blocks. Dialog objects hold OS resources; Using ensures cleanup.
  • Test on high-DPI monitors. Dialogs render differently at 125% and 150% scaling.

Working code example

Full working example showing Dialog Boxes in a real Windows Forms context:

Private Sub btnPick_Click(sender As Object, e As EventArgs) Handles btnPick.Click
    Using dlg As New OpenFileDialog()
        dlg.Title = "Select a file"
        dlg.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
        dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)

        If dlg.ShowDialog() = DialogResult.OK Then
            Dim contents As String = System.IO.File.ReadAllText(dlg.FileName)
            txtOutput.Text = contents
        End If
    End Using
End Sub

Troubleshooting common issues with Dialog Boxes

  • Dialog does not appear. Confirm ShowDialog() is called on the UI thread. Cross-thread invocations must marshal via Control.Invoke().
  • File permission errors. Wrap file operations in Try/Catch. UAC-protected folders throw UnauthorizedAccessException.
  • Filter string ignored. Filters use the exact “Description|*.ext|Description2|*.ext2” format. A single stray character breaks parsing silently.
  • Font not applied. After dialog closes, assign dlg.Font to the target control’s Font property explicitly.
  • Print job silently drops. Confirm PrinterSettings.IsValid before calling Print().

Frequently Asked Questions

What is the Dialog Boxes in VB.NET?
The Dialog Boxes in VB.NET is a modal Windows Forms dialog that lets users pick a value (file, font, color, or printer setting) without writing custom UI code. It ships with the .NET Framework in the System.Windows.Forms namespace.
How do I show the Dialog Boxes in code?
Create an instance, set its default properties, and call ShowDialog(). Wrap it in a Using block so system resources are released even if the user closes the dialog with the X button.
How do I know if the user clicked OK on the Dialog Boxes?
ShowDialog() returns a DialogResult enumeration. Compare against DialogResult.OK before reading the dialog’s property values, otherwise you may end up processing an unpicked or default value.
Why does the Dialog Boxes appear behind my form?
Pass Me (the current form) as the owner argument: dlg.ShowDialog(Me). Without an owner, the dialog can appear behind other windows or be misplaced across multi-monitor setups.
Can I customize the Dialog Boxes appearance?
The built-in dialogs are OS-provided and only offer limited customization through their properties. For deeper styling you must write a custom form that mimics the dialog behavior.
Angel Jude Suarez


Full-Stack Developer at PIES IT Solution

Focuses on Python development, machine learning, and AI integration. Has built production AI systems including OpenAI Whisper integration for medical transcription and GPT-4o-powered diagnosis assistance. Strong background in pandas, scikit-learn, and TensorFlow.

Expertise: Python · PHP · Java · VB.NET · ASP.NET · Machine Learning · AI Integration · OpenCV · Django · CodeIgniter
 · View all posts by Angel Jude Suarez →

Leave a Comment