You are on page 1of 13

Exploring the Dialogs Controls in Vb.

Net
(Page 1 of 4 )

Dialog-box controls are an important part of programs, especially if you expect users to interact
with your application. This article explains how to handle several different dialog-box controls.
Dialogs controls, or more correctly dialog-box controls, are controls that allow the user to
interact with the program and retrieve information. They usually pop up when they are called,
and allow you to choose a color from a color-pick dialog, or open or save a file through a dialog,
and so forth. The user interacts with these at run time, and they present an interface from which
the choice is made. They are related to the CommonDialog controls in VB6 and function in a
similar way. However they are now based on well defined classes in the System.Windows.Forms
name space as we shall see in this tutorial.
In the course of this tutorial you will see how they fit into the class structure. You will also try to
understand their usage by means of an example which uses some of the properties and methods
of these controls.
Accessing the controls in the IDE
When you create a WindowsApplication project and add a form to the project, you can see these
controls in the Toolbox as shown in this picture. Besides these dialog-box controls there are three
other dialog-box controls related to printing. These are not covered in this tutorial. While these
are built-in controls, it is also possible to create user defined or custom dialog-box controls.
Custom dialog-box controls are also not considered.

These controls are like any other controls; they can be dragged and dropped on the form.
However, they are not visible items and find themselves in a tray below the form as shown in
this picture, where one each of these controls have been placed on the form, Form1.
FontDialog Control
As the name implies, this control, when called up, brings up a familiar dialog box, Font, from
which a variety of choices regarding fonts can be made as shown in this picture. When you click
OK to this screen, your font choices will be applied to a string in some part of your program.

This next picture shows you the class view of the FontDialog class with all the members shown
on the right. You can use the New() method to instantiate this object and set its properties and
methods. You may notice its hierarchical relationship with the CommonDialog controls.
Create a WindowsApplication Project, add a form to it and name it suitably. Add a button and a
text box control to the form. Drag and drop a FontDialog Control onto the form and it will
immediately move to the tray underneath the form. To the click event of the button type in the
following code:
Private Sub Button2_Click(ByVal sender As System.Object,

ByVal e As _ System.EventArgs) Handles Button2.Click


FontDialog1.ShowDialog()

'FontDialog window pops-up If Windows.Forms.DialogResult.OK Then

Me.TextBox1.Text = "Viva la Republica!"


Me.TextBox1.Font = FontDialog1.Font End If End Sub
When you build the project and run the program the form pops up. If you now click on the button
which has the above code in its click event, the FontDialog window shown earlier pop ups.
When you make your choice and click on the OK button your choice will be applied to the text
box's font. FontDiaog1.Font has all the selected features. For the picture shown next the choices
were, Font: Lucida Console, FontStyle: Bold, Size:11, and Effects: Underline as seen in the next
picture. Of course the text box size will not accommodate whatever size you choose, although at
design-time the text box size is related to your choice of the font, with the default being
Microsoft MS.
Exploring the Dialogs Controls in Vb.Net -
ColorDialog Control
(Page 2 of 4 )

Again as the name suggests, when this control is called up, it will show a color-pick dialog,
Color, where you can pick a color that can be applied to the object's color related properties.
This can be the backcolor of a text box for example, or the form's backcolor, or forecolor, and so
forth.
The next picture shows the class view of the ColorDialog class. Again you can trace it back to
the CommonDialog control in the inheritance chain. There are many properties, out of which
only the Color() property is used. The ShowDialog() is however inherited from the
CommonDialog.

Now to the above project add another button, and after dragging and dropping a ColorDialog
control onto the form, add the following code to the click event of the button:
Private Sub Button1_Click(ByVal sender As System.Object,

ByVal e As _ System.EventArgs) Handles Button1.Click


ColorDialog1.ShowDialog() If Windows.Forms.DialogResult.OK

Then Me.TextBox1.BackColor = ColorDialog1.Color End If End Sub


The first line of code, ColorDialog1.ShowDialog() pops up the Color pick window shown
earlier. When you make the choice and click OK, the Windows form gets the message and
applies the ColorDialog's Color() property (the color chosen) to the Backcolor property of the
text box control. When you build and run the form, it shows with the two buttons, one for the
font and one for the color. Clicking the Font button creates some text in the text box, and by
clicking the color button and applying a light blue color you will see the following.

FolderBrowserDialog, OpenFileDialog and SaveFileDialog Controls


These dialogs will be briefly discussed, and in the last part of the project all these controls will
be used, which accomplishes the following. The FolderBrowserDialog Control will open a
browser from which a folder will be chosen. The OpenFileDialog will open a file from the folder
and will be passed on to a web browser control to display a web page. The chosen file can then
be saved to a chosen location using the SaveFileDialog control.
FolderBrowserDialog Control
When you call up this control it will pop up a Browse For Folder dialog as shown in the next
picture, from which you can choose a folder, a sub-folder and any other nested folder. The path
to this folder is now captured in this object's SelectedPath() property which can be accessed by
code.
This next picture shows you the class view of the FolderBrowserDialog class and its members in
the right panel. You can instantiate using the NEW() keyword.

Exploring the Dialogs Controls in Vb.Net -


OpenFileDialog Control
(Page 3 of 4 )

When this control's ShowDialog() method is called, an Open dialog window pops up as shown
in the next picture. As we shall see later in the example, you can filter the file extension you
want opened; that is the reason it is showing (*.htm;*. html) as the extensions.

This next picture shows the class view of this control. However we will be mostly using the
properties and methods of the FileDialog class.

The properties used for the example shown, such as the filter for file extension, come because
they are inherited from the FileDialog Class as shown here.
SaveFileDialog Control
When this control's ShowDialog() method is called, the Save As window shows up as shown
here.

It is possible to code such that a particular file type can be saved to a chosen directory using
again the filter for file extension property derived from the FileDialog class. Both this control, as
well as the OpenFileDialog control, give access to the OpenFile() method (as System.IO.Stream)
which can be very useful.
Exploring the Dialogs Controls in Vb.Net -
Usage of these Controls
(Page 4 of 4 )

To the project already created add a WebBrowser control. If this is not already in the toolbox,
you may add it to the toolbox from Tools -->ChooseToolBoxItems... from the main menu which
opens up this next dialog where you can choose the WebBrowser control as shown. When you
click OK, after placing a check mark this control will be added to the toolbox, from where you
can drag and drop it onto your form. You should also drag and drop the the three controls,
OpenFileDialog, SaveFileDialog, and the FolderBrowserDialog.

Add one more button to the form, and the finished form should appear as shown in the next
picture.
Now to the click event of the button marked "Show selected file in browser " add the following
code:
'This event opens a dialog from which a folder is chosen. After the folder

'is chosen, the OpenFileDialog properties such as Filter, InitialDirectory

'are set and then the ShowDialog() is called. When this window is closed

'with an 'OK' choice, the WebBrowser navigates to the folder and then on to

'the filename and gets rendered in the WebBrowser control shown as a large

'white area in the form. It is assumed that you will browse to get a web

'page with an *.htm extension. This is then followed by a call to the

'SaveToLocal() procedure

Private Sub Button3_Click(ByVal sender As

System.Object, ByVal e As _
System.EventArgs) Handles Button3.Click
FolderBrowserDialog1.ShowDialog()
If Windows.Forms.DialogResult.OK Then
If Windows.Forms.DialogResult.OK Then
OpenFileDialog1.Filter = "(*.htm;*.html)|*.htm;*.html)"
OpenFileDialog1.InitialDirectory = _
FolderBrowserDialog1.SelectedPath
OpenFileDialog1.ShowDialog()
If Windows.Forms.DialogResult.OK Then
WebBrowser1.Navigate(FolderBrowserDialog1.SelectedPath _
& "" & OpenFileDialog1.FileName)
Call SavetoLocal(OpenFileDialog1.FileName)
End If
End If
End If
End Sub

'this procedure saves the file to a browsed folder. Although the

'InitialDirectory property has been set, it appears not to accept it.

'However, you can choose the directory where you want to it to be saved.

Private Sub SaveToLocal (ByVal str1 As String)


SaveFileDialog1.Filter = "(*.htm)|*.htm"
SaveFileDialog1.InitialDirectory = "C:"
SaveFileDialog1.FileName = str1
SaveFileDialog1.ShowDialog()
End Sub
After you build the project and run the form you will see the following. The WebBrowser control
renders the chosen HTML file. The SaveFileDialog will save this file to a chosen location which
may be verified.

Microsoft documentation shows DialogResult.OK as a result of the Dialog control's


ShowDialog() method. However DialogResult is an enum, and the qualifying expression will not
be evaluated in their many examples. Windows.Forms.DialogResult.OK is the proper expression
to use, as in this tutorial.
Summary
All the dialog-box controls that you find under Dialogs in the ToolBox were described, and the
examples show some of the methods and properties for these controls. These controls are very
useful as they confer a great deal of flexibility in programming. Object browser is an invaluable
aid in understanding the hierarchies and the inheritance chains, and you must review it as often
as you can. You may also review online help when you want to look for more specifics.

You might also like