There are many built-in dialog boxes to be used in Windows forms
for various tasks like opening and saving files, printing a page,
providing choices for colors, fonts, page setup, etc., to the user of
an application. These built-in dialog boxes reduce the developer's
time and workload.
All of these dialog box control classes inherit from
the CommonDialog class and override the RunDialog() function of the
base class to create the specific dialog box.
The RunDialog() function is automatically invoked when a user of a
dialog box calls its ShowDialog() function.
The ShowDialog method is used to display all the dialog box controls
at run-time. It returns a value of the type
of DialogResult enumeration. The values of DialogResult enumeration
are −
Abort − returns DialogResult.Abort value, when user clicks an
Abort button.
Cancel − returns DialogResult.Cancel, when user clicks a
Cancel button.
Ignore − returns DialogResult.Ignore, when user clicks an
Ignore button.
No − returns DialogResult.No, when user clicks a No button.
None − returns nothing and the dialog box continues running.
OK − returns DialogResult.OK, when user clicks an OK button
Retry − returns DialogResult.Retry , when user clicks an Retry
button
Yes − returns DialogResult.Yes, when user clicks an Yes button
The following diagram shows the common dialog class inheritance −
All these above-mentioned classes have corresponding controls that
could be added from the Toolbox during design time. You can
include relevant functionality of these classes to your application,
either by instantiating the class programmatically or by using
relevant controls.
When you double click any of the dialog controls in the toolbox or
drag the control onto the form, it appears in the Component tray at
the bottom of the Windows Forms Designer, they do not directly
show up on the form.
The following table lists the commonly used dialog box controls.
Click the following links to check their detail −
ColorDialog
It represents a common dialog box that displays available colors along with controls that enable
the user to define custom colors.
FontDialog
It prompts the user to choose a font from among those installed on the local computer and lets
the user select the font, font size, and color.
OpenFileDialog
It prompts the user to open a file and allows the user to select a file to open.
SaveFileDialog
It prompts the user to select a location for saving a file and allows the user to specify the name
of the file to save data.
PrintDialog
It lets the user to print documents by selecting a printer and choosing which sections of the
document to print from a Windows Forms application.
VB.Net Color Dialog
Properties of the ColorDialog Control
The following are some of the commonly used properties of the ColorDialog control –
Methods of the ColorDialog Control
The following are some of the commonly used methods of the ColorDialog control –
Example
In this example, let’s change the forecolor of a label control using the color dialog box. Take the following
steps –
Drag and drop a label control, a button control and a ColorDialog control on the form.
Set the Text property of the label and the button control to ‘Give me a new Color’ and ‘Change Color’,
respectively.
Change the font of the label as per your likings.
Double-click the Change Color button and modify the code of the Click event.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If ColorDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
Label1.ForeColor = ColorDialog1.Color
End If
End Sub
When the application is compiled and run using Start button available at the Microsoft Visual Studio tool
bar, it will show the following window –
VB.Net Color Dialog Result
Clicking on the Change Color button, the color dialog appears, select a color and click the OK button. The
selected color will be applied as the forecolor of the text of the label.
FONT DIALOG
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If FontDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
RichTextBox1.ForeColor = FontDialog1.Color
RichTextBox1.Font = FontDialog1.Font
End If
End Sub
OpenFileDialogue
Example
In this example, let’s load an image file in a picture box, using the open file dialog box. Take the following
steps –
Drag and drop a PictureBox control, a Button control and a OpenFileDialog control on the form.
Set the Text property of the button control to ‘Load Image File’.
Double-click the Load Image File button and modify the code of the Click event:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If OpenFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
End If
End Sub
When the application is compiled and run using Start button available at the Microsoft Visual Studio tool
bar, it will show the following window −
Public Class Picturebx
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
‘Dim Str As String = “C:\Users\AMIT YADAV\Desktop\”
PictureBox1.Image = Image.FromFile(“C:\Users\AMIT YADAV\Desktop\jtp2.png”)
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
PictureBox1.Height = 250
PictureBox1.Width = 400
Label1.Visible = False
End Sub
Private Sub Picturebx_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Text = “javaTpoint.com” ‘Set the title name for the form
Button1.Text = “Show”
Label1.Text = “Click to display the image”
Label1.ForeColor = ForeColor.Green
End Sub
End Class
COMBOBOX
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
‘ Set the caption bar text of the form.
Me.Text = “tutorialspont.com”
End Sub
‘sends the selected items to the list box
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If ComboBox1.SelectedIndex > -1 Then
Dim sindex As Integer
Sindex = ComboBox1.SelectedIndex
Dim sitem As Object
Sitem = ComboBox1.SelectedItem
ListBox1.Items.Add(sitem)
End If
End Sub
‘populates the list
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ComboBox1.Items.Clear()
ComboBox1.Items.Add(“Safety”)
ComboBox1.Items.Add(“Security”)
ComboBox1.Items.Add(“Governance”)
ComboBox1.Items.Add(“Good Music”)
ComboBox1.Items.Add(“Good Movies”)
ComboBox1.Items.Add(“Good Books”)
ComboBox1.Items.Add(“Education”)
ComboBox1.Items.Add(“Roads”)
ComboBox1.Items.Add(“Health”)
ComboBox1.Items.Add(“Food for all”)
ComboBox1.Items.Add(“Shelter for all”)
ComboBox1.Items.Add(“Industrialisation”)
ComboBox1.Items.Add(“Peace”)
ComboBox1.Items.Add(“Liberty”)
ComboBox1.Items.Add(“Freedom of Speech”)
ComboBox1.Text = “Select from…”
End Sub
‘sorting the list
Private Sub Button3_Click(sender As Object, e As EventArgs)
ComboBox1.Sorted = True
End Sub
‘clears the list
Private Sub Button4_Click(sender As Object, e As EventArgs)
ComboBox1.Items.Clear()
End Sub
‘displaying the selected item on the label
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) _
Handles ListBox1.SelectedIndexChanged
Label1.Text = ComboBox1.SelectedItem.ToString()
End Sub
End Class