Windows operating system, we often see a set of commonly
used dialog boxes, such as Open dialog box. For example, when you choose File -> Open of WordPad or File->Open of Notepad, you see the same dialog box with the same interface. This
is so because all these applications make use of common dialog boxes developed
by Microsoft. That means we can use all these common dialog boxes without
having to create these dialog boxes from scratch. That not only saves a lot of time, it also
allows you to provide standard interface to users.
Now let us understand how to use these common dialog boxes
in Visual Basic.
Common Dialog Control
Visual Basic allows your application to use common dialog
boxes trough an ActiveX control called “Common Dialog Control”. This is not a standard control that you find
in toolbox by default. Instead you have to load this control into your project.
What is an ActiveX Control?
For the time being it will suffice to know that an ActiveX
control is a control that is not part of intrinsic controls that you find in
Toolbox. And an ActiveX control is a
user-defined control developed using ActiveX technology. These controls come in
the form of .OCX file. Each .OCX file may contain one or more ActiveX controls.
Each control is assigned a particular task. There are hundreds of ActiveX
controls in the market today. And surely more will come in future. It is also possible for a Visual Basic
programmer to create his own ActiveX control and distribute to other developers
(who may use any tools that is supporting ActiveX).
Note: If you want to use an ActiveX
control, first you must install ActiveX control in question properly and then
load it into Visual Basic project. When you install Visual Basic, it installs
good number of ActiveX controls into your machine. So all that you have to do
is load them into your project and start using them.
Note: We will discuss more about
ActiveX technology and how to create our own ActiveX control in chapter 27,28
and 29.
How to load an ActiveX control into Visual Basic project?
The following procedure is to be followed to load an ActiveX
control into a Visual Basic project.
1.
Select Project
-> Components in Visual Basic IDE
Visual Basic displays a window that contains the list of
ActiveX controls that are installed in your machine. See figure 9.1.
2.
Make sure Control
tab is selected.
3.
Go to the control that you want to load. In this case
we want to load Microsoft Common Dialog
Control 6.0, so select that control.
4.
Turn on the checkbox on the left of the control.
At this stage, the name and path of the selected .OCX file
will be displayed at the bottom of Component dialog. See figure 9.1.
5.
Click on Ok
button.
Once an ActiveX control is loaded, one or more icons are
displayed depending upon whether the selected component contains one or more
controls. See figure 9.2.
Figure 9.1: Components
window displaying list of ActiveX controls.
What does Common dialog control have?
Microsoft common dialog control allows you to access a set
of dialog boxes through its methods and properties.
The following are the dialog boxes that can be accessed
through Common dialog control.
Dialog box
|
Meaning
|
Method to Invoke
|
Open
|
Displays the list of files and
directories and allows you to select a file.
|
ShowOpen
|
Save As
|
Displays the list of files and
directories and allows you to select a file.
|
ShowSave
|
Color
|
Displays a list of standard
colors and System colors.
|
ShowColor
|
Font
|
Displays a dialog box with the
list of available fonts and styles.
|
ShowFont
|
Print
|
Displays print dialog box
|
ShowPrinter
|
Help
|
Invokes help window using the
given file.
|
ShowHelp
|
Table 9.1: Dialog
boxes that can be accessed through common dialog control.
Now, let us understand properties related to each of these
common dialog boxes.
Figure 9.2:
Common dialog control icon in toolbox.
Open and Save As dialog boxes
Open dialog box
is used to let user select a file. It contains list of files, directories and
also a text box into which you can enter filename.
Save As dialog
box is same as Open dialog box
except the caption. Both the dialog boxes display list of files, directories
and drives so that user can select a file from anywhere from the file system.
The following are the properties that are related to Open and Save As dialog boxes.
Property
|
Meaning
|
CancelError
|
Specifies whether a trappable runtime error (with number
32755) occurs when user selected Cancel
button or no error occurs.
|
DefaultExt
|
Specifies the default extension, such as .txt or .frm for
files. When a file with no extension is entered the default extension is
automatically added.
|
DialogTitle
|
Contains the title of the dialog box. This is ignored for
Font, Color and Print dialog boxes.
|
FileTitle
|
Returns only the filename without path.
|
Filter
|
Specifies the filters to be displayed in Type list box of dialog.
|
FilterIndex
|
Specifies the default filter.
|
InitDir
|
Specifies the directory to be used initially. If not
specified then current directory is used.
|
FileName
|
Returns the path and filename of the selected file.
|
Table 9.2:
Properties that are specific to Open and Save As dialog boxes.
The following code listing shows you how to use Open dialog
box.
‘ Open button displays Open dialog box and takes the filename
Private Sub
CmdOpen_Click()
With CommonDialog1
‘ set two filters;
one is *.txt another is *.*
.Filter = "Text
Files|*.txt|All files|*.*"
.FilterIndex = 0 ' first filter is default
.DialogTitle =
"Select a text file"
‘ invoke Open dialog
box
.ShowOpen
' print filename on
the form
Print .FileName
End With
End sub
Listing 9.1: Displaying
Open dialog box to take a filename from user.
If you run the above code, you will see a dialog box as
shown figure 9.3.
Figure 9.3: Open
dialog box when invoked.
Font Dialog box
Font dialog box displays the list of available font names
and other font styles. This dialog box can display the fonts supported by
either Printer or Screen or Both.
Note: You must specify whether you want
to list fonts supported by Screen or Printer or Both. Unless you specify this,
no fonts will be displayed in Font dialog box.
The following are the properties related to Font dialog box.
Property
|
Meaning
|
FontBold
|
Whether bold was selected
|
FontItalic
|
Whether italic
was selected.
|
FontStrikethru
|
Whether
|
FontUnderline
|
Whether underline was
selected.
|
FontName
|
The selected font name
|
FontSize
|
The selected font size.
|
Table 9.3:
Properties that are specific to Font dialog box.
Flags property for Font Dialog box
This property is used to set options related to Font dialog
box. You can set more than one flag
using OR operator.
The following is the list of flags related to Font dialog
box.
Flag Constant
|
Value
|
Meaning
|
CdlCFBoth
|
&H3
|
Causes fonts supported by
Screen and Printer to be listed.
|
CdlCFEffects
|
&H100
|
Causes the dialog box to
display Underline and Strikethrough effects also.
|
CdlCFPrintersFonts
|
&H2
|
Causes only fonts supported by
printer to be listed.
|
CdlCFScreenFonts
|
&H1
|
Causes only fonts supported by
screen to be listed.
|
Table 9.4: Flags
related to Font dialog box
Please see on-line documentation for complete list of flags.
The following listing illustrates how to use Font dialog box
to allow user to select Font related options and set those options to current
form.
Private Sub
cmdFont_Click()
With CommonDialog1
'Display screen
fonts with effects
.Flags =
cdlCFScreenFonts Or cdlCFEffects
.ShowFont
Me.FontName =
.FontName
Me.FontSize =
.FontSize
Me.FontBold =
.FontBold
Me.FontItalic =
.FontItalic
Me.FontUnderline =
.FontUnderline
Me.FontStrikethru =
.FontStrikethru
Print "This is
sample text"
End With
End Sub
Listing 9.2:
Displaying Font dialog box.
Color Dialog box
Color dialog box displays color palette from which user can
select a color. Color dialog box also allows users to create a custom color and
select it.
Color property
returns the selected color. Flags property can be used to specify
whether user should be allowed to define custom colors (cdlCGPreventFullOpen)
and whether full dialog box (including Define
custom colors section) is to be displayed.
The following code displays Color dialog box and changes the
background of the form with the color selected by user.
Private Sub
cmdBKColor_Click()
With CommonDialog1
.Flags =
cdlCCFullOpen
.ShowColor
Me.BackColor =
.Color
End With
End Sub
Listing 9.3: Invoking Color dialog box.
Figure 9.4: Font
dialog box.
Figure 9.5: Color
dialog box with Define custom color section open.
Print Dialog Box
The Print dialog box allows the user to specify how output
should be printed. The user can specify a range of pages to be printed, print
quality, number of copies to be printed, and so on.
Note: Print dialog box allows user to
select the option only. It does not send the data to printer. To send data to
printer use Printer object.
The following are the properties related to Print dialog
box.
Property
|
Meaning
|
Copies
|
Specifies the number of copies to be printed.
|
FromPage
|
Specifies the value to be displayed in FromPage text box Print dialog box.
|
ToPage
|
Specifies the value to be displayed in ToPage text box Print dialog box.
|
Min
|
Specifies the minimum value for page range.
|
Max
|
Specifies the maximum value for page range.
|
PrinterDefault
|
Specifies whether the options selected in Print dialog box
change the settings of Printer
system object.
|
Table 9.5:
Properties of Print dialog box.
Please see on-line documentation for flags that are
applicable to Print dialog. The following code allows user to select printing
related options and also changes the corresponding settings in Printer system
object.
Private Sub
CmdPrint_Click()
With CommonDialog1
' enable changes to
printer object
.PrinterDefault = True
' you must set Min
and Max properties otherwise
' Frompage and ToPage
are ignored.
.Min = 1
.Max = 10
.FromPage = 5
.ToPage = 10
' select Pages as the default
.Flags =
cdlPDPageNums
.ShowPrinter
' handle printing
using Printer Object
End With
End Sub
Listing 9.4:
Printing using Print dialog box.
When the above code is run the displayed Print dialog should
look like figure 9.6.
Figure 9.6: Print
dialog box.
Help Dialog Box
Unlike other dialog boxes, it doesn’t take any input from
user. Instead it runs WINHLP32.EXE
program to display help file that is set using HelpFile property.
The following are the properties related to Help dialog box.
Property
|
Meaning
|
Helpfile
|
Specifies the help file to be used to display help.
|
Helpkey
|
Specifies the keyword of the topic that is to be
displayed.
|
Helpcommand
|
Specifies the type of online help requested. Please see
on-line documentation for valid options.
|
Table 9.6:
Properties related to Help dialog box of Common dialog control
The following listing displays the help file CDPLAYER.HLP
that is found in Windows\Help folder.
Private Sub
Cmdhelp_Click()
With CommonDialog1
.FileName =
"c:\windows\help\cdplayer.hlp"
' display contents of
the file initially
.HelpCommand = cdlHelpContents
.ShowHelp
End With
End Sub
Listing 9.5: Code
to display help using common dialog control.
No comments:
Post a Comment