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

What is Dialog Boxes in VB.net?

A 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, an acknowledgement 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 user clicks an Abort button.

2. Cancel

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

3. Ignore

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

4. No

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

5. None

None returns nothing and the dialog box continues running.

6. OK

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

7. Retry

Retry returns DialogResult.Retry , when user clicks an Retry button

8. Yes

Yes returns DialogResult.Yes, when user clicks an 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.


Leave a Comment