Professional Documents
Culture Documents
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,
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,
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
'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
'SaveToLocal() procedure
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
'However, you can choose the directory where you want to it to be saved.