The use of CommonDialog control in VB (I.)

Source: Internet
Author: User
Tags filter exit goto

Using the CommonDialog control

The CommonDialog control provides a standard set of dialog boxes for actions such as opening and saving files, setting printing options, selecting colors and fonts, and so on. When you run the Windows help engine, the control can also display help.

The CommonDialog control provides an interface between Visual Basic and the Microsoft Windows Dynamic Connection Library Commdlg.dll routines. In order to create a dialog box with this control, you must require Commdlg.dll to be in the Microsoft Windows \system directory.
In order to use the CommonDialog control in your application, you should add it to the form and set the properties. The dialog that the control displays is determined by the method of the control. At run time, a dialog box or execution help engine is displayed when the appropriate method is called, and the CommonDialog control is displayed as an icon on the form at design time. The size of this icon cannot be changed.
The CommonDialog control can display the following common dialog boxes:
Open
"Save As"
Color
Font
Print
To use the CommonDialog control
1. If you do not add a CommonDialog control, you should add the control to the toolbox by selecting "part" from the "Project" menu. In the markup dialog, locate and select the control in the control, and then click the OK button.
2. Click the CommonDialog control in the toolbox and draw the control on the form. When you draw a CommonDialog control on a form, the control is automatically resized. Like the timer control, the CommonDialog control is not visible at run time.
3. At run time, use the methods listed in the following table to display the required dialogs.

Display the Open and Save As dialog boxes
You can specify a drive, directory, file extension, and file name with the Open dialog box. The Save As dialog box looks the same as the Open dialog box, except that the title and filename of the dialog box are dimmed. When the file is selected at run time and the dialog box closes, the FileName property can be used to get the selected file name.
To display the Open dialog box
1. Specifies the list of file filters to display in the File Type list box.
You can set the Filter property in the following formats:
Description1 | Filter1 | Description2 | Filter2 ...
Description is the string displayed in the list box-for example, "Text Files (*.txt)." Filter is the actual file filter-for example, "*.txt". Each description | The filter settings must be delimited (|) by a pipe symbol.
2. Display the dialog box with the ShowOpen method.

The FileName property can be used to get the name of the selected file after the file is selected.
For all public dialog boxes, an error is generated when the CancelError property is True and the user clicks the Cancel button in the dialog box. Catch an error while displaying the dialog box to detect if the Cancel button is pressed.
The following code displays the Open dialog box with the selected file name as the parameter of the open file procedure:
Private Sub Mnufileopen_click ()
' CancelError is True.
On Error GoTo ErrHandler
' Set the filter.
Commondialog1.filter = "All Files (*.*) |*.*| Text _
Files (*.txt) |*.txt| Batch Files (*.bat) |*.bat "
' Specifies the default filter.
Commondialog1.filterindex = 2
' Display the Open dialog box.
Commondialog1.showopen
' Invokes the process of opening the file.
OpenFile (Commondialog1.filename)
Exit Sub

ErrHandler:
' The user presses the Cancel ' button.
Exit Sub
End Sub

Use the Color dialog box
Use the Color dialog box to select a color in the palette, or create and select a custom color. At run time, the Color property is used to get the selected color when the colors are selected and the dialog box closes.

To display the Color dialog box
1. Set the Flags property of the CommonDialog control to the Visual Basic constant Cdlccrgbinit.
2. Display the dialog box with the Showcolor method.
Use the Color property to get the RGB value of the selected color. When you click the Command1 command button, the following code displays the Color dialog box:
Private Sub Command1_Click ()
' Set Cancel to True.
Commondialog1.cancelerror = True
On Error GoTo ErrHandler
' Set the Flags property.
Commondialog1.flags = Cdlccrgbinit
' Display the Color dialog box.
Commondialog1.showcolor
' Sets the background color of the form to the selected color.
Form1.backcolor = Commondialog1.color
Exit Sub

ErrHandler:
' The user presses the Cancel ' button.
Exit Sub
End Sub

Using the Font dialog box
The Font dialog box selects fonts based on size, color, and style. Once the user has selected a font in the Font dialog box, the following properties contain information about the user's options.

To display the Font dialog box
1. Set the Flags property to one of the following Visual Basic constants:
Cdlcfscreenfonts (screen font)
Cdlcfprinterfonts (printer Fonts)
Cdlcfboth (can be both a screen font and a printer font)
Warning you must set the Flags property to one of these values before displaying the Font dialog box, otherwise there will be no error in the font.
2. Display the dialog box with the Showfont method.
The following code sets the font properties of the text box based on the user's selection in the Font dialog box:
Private Sub Command1_Click ()
' Set Cancel to True.
Commondialog1.cancelerror = True
On Error GoTo ErrHandler
' Set the Flags property.
Commondialog1.flags = Cdlcfboth Or cdlcfeffects
' Display the Font dialog box.
Commondialog1.showfont
' Sets the Text property according to the user's choice.
Text1.Font.Name = Commondialog1.fontname
Text1.Font.Size = Commondialog1.fontsize
Text1.Font.Bold = Commondialog1.fontbold
Text1.Font.Italic = Commondialog1.fontitalic
Text1.Font.Underline = Commondialog1.fontunderline
Text1.fontstrikethru = Commondialog1.fontstrikethru
Text1.forecolor = Commondialog1.color
Exit Sub
ErrHandler:
' The user presses the Cancel ' button.
Exit Sub
End Sub

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.