5
Common Dialog Control Common Dialog Control The common dialog box in visual basic is an insertable control that allows users to display a number of common dialog boxes in their program. These include Open and Save As file dialog boxes; the Find and Replace editing dialog boxes; the Print, Print Setup, Print Property Sheet, and Page Setup printing dialog boxes; and the Color and Font dialog boxes .The easiest way to access these controls is to use a component called Microsoft Common Dialog Control 6.0. To add this component to your project, choose Project - Components, and mark it in the controls list. Subsequently, a new control should appear in your control toolbox. Choose it, and place the control onto your form (note it's only visible in design time). Open and Save dialogs ShowOpen shows a dialog that lets the user choose a drive, directory, file name and file extension to (presumably) open a file. The save dialog is identical in apperance and function, with the exception of the caption. At run time, when the user chooses a file and closes the dialog box, the FileName property is used to get the selected file name. Before displaying the dialog Before you can use these dialogs, you must first set the Filter property to allow the user to select a file extension . The filter property is a string with the following structure: description1|filter1|description2|filter2| Here, the description is the string that shows up in the list box of all the available filters to choose from. set default extension using DefaultExt). Set default initial directory using initdir code COMMONDIALOG1.Filter = "textfiles|*.txt|documents|*.doc|allfiles| *.*"

Common dialog control

Embed Size (px)

Citation preview

Page 1: Common dialog control

Common Dialog ControlCommon Dialog Control The common dialog box in visual basic is an insertable control that allows users to display a number of common dialog boxes in their program.

These include Open and Save As file dialog boxes; the Find and Replace editing dialog boxes; the Print, Print Setup, Print Property Sheet, and Page Setup printing dialog boxes; and the Color and Font dialog boxes

.The easiest way to access these controls is to use a component called Microsoft Common Dialog Control 6.0. To add this component to your project, choose Project - Components, and mark it in the controls list. Subsequently, a new control should appear in your control toolbox. Choose it, and place the control onto your form (note it's only visible in design time).

Open and Save dialogs

ShowOpen shows a dialog that lets the user choose a drive, directory, file name and file extension to (presumably) open a file.

The save dialog is identical in apperance and function, with the exception of the caption.

At run time, when the user chooses a file and closes the dialog box, the FileName property is used to get the selected file name.

Before displaying the dialog Before you can use these dialogs, you must first set the Filter property to allow the user to select a file extension . The filter property is a string with the following structure: description1|filter1|description2|filter2|

Here, the description is the string that shows up in the list box of all the available filters to choose from.

set default extension using DefaultExt).

Set default initial directory using initdir

code

COMMONDIALOG1.Filter = "textfiles|*.txt|documents|*.doc|allfiles|*.*"

COMMONDIALOG1.FileName = "text1"

COMMONDIALOG1.InitDir = "D:\"

COMMONDIALOG1.ShowOpen

RICHTEXTBOX1.FileName = COMMONDIALOG1.FileName

Color dialog

The color dialog allows the user to select a color from both a palette as well as through manual choosing using RGB .

Page 2: Common dialog control

You retrieve the selected color using the Color property.

Flags used in color dialog

Constant Value

Description

cdlCCFullOpen &H2 Entire dialog box is displayed, including the Define Custom Colors section.

cdlCCHelpButton &H8 Causes the dialog box to display a Help button.

cdlCCPreventFullOpen

&H4 Disables the Define Custom Colors command button and prevents the user from defining custom colors.

cdlCCRGBInit &H1 Sets the initial color value for the dialog box.

Sample code

COMMONDIALOG1.Flags = cdlCCFullOpen

COMMONDIALOG1.ShowColor

RICHTEXTBOX1.SelColor = COMMONDIALOG1.color

Font dialog

The Font dialog box allows the user to select a font by its size, color, and style.

Once the user makes selections in the Font dialog box, the following properties contain information about the user's selections.

Color The selected color.

FontBold Returns whether the bold checkbox was selected.

FontItalic Returns whether the italic checkbox was selected.

FontStrikethru Returns whether the strikethrough checkbox was selected. To use this property, you must first set the Flags property to cdlCFEffects.

Page 3: Common dialog control

FontUnderline Returns whether the underline checkbox was selected.

FontName Returns the selected font name.

FontSize Returns the selected font size.

Before displaying the dialog To display the dialog, you must first set the Flags property either cdlCFScreenFonts or cdlCFPrinterFonts or cdlCFBoth, if you intend to let the user choose between both screen fonts and printer fonts

code

COMMONDIALOG1.Flags = cdlCFBoth + cdlCFEffects

COMMONDIALOG1.ShowFont

RICHTEXTBOX1.SelFontName = COMMONDIALOG1.FontName

RICHTEXTBOX1.SelFontSize = COMMONDIALOG1.FontSize

RICHTEXTBOX1.SelBold = COMMONDIALOG1.FontBold

RICHTEXTBOX1.SelItalic = COMMONDIALOG1.FontItalic

RICHTEXTBOX1.SelStrikeThru = COMMONDIALOG1.FontStrikethru

Print dialog

The print dialog box allows the user to select how output should be printed.

Options and properties accessible to the user includes the amount of copies to make, print quality, the range of pages to print, etc.

Copies The amount of copies to print.

FromPage The starting page of the print range.

ToPage The ending page of the print range.

hDC The device context for the selected printer.

Orientation The page's orientation (as in portrait or landscape).

Before displaying the dialog Before showing the dialog, feel free to set the appropriate print dialog properties to set the default values to use.

Sample code

CommonDialog1.ShowPrinter

BeginPage = CommonDialog1.FromPage

Page 4: Common dialog control

EndPage = CommonDialog1.ToPage

NumCopies = CommonDialog1.Copies

Orientation = CommonDialog1.Orientation

Rich Textbox Control

• The rich textbox control allows the user to enter and edit text, providing more advanced formatting features than the conventional textbox control. You can use different fonts for different text sections.

You can even control indents, hanging indents, and bulleted paragraphs. This control is loaded by selecting the Microsoft Rich Textbox Control from the Components dialog box.

Most of the properties, events, and methods associated with the conventional textbox are available with the rich text box. A major difference between the two controls is that with the rich textbox, multiple font sizes, styles, and colors are supported.

Some unique properties of the rich textbox are:

FileName Can be used to load the contents of a .txt or .rtf file into the control.

SelFontName Set the font name for the selected text.

SelFontSize Set the font size for the selected text.

SelFontColor Set the font color for the selected text.

selBold,selItalic,set selected text to bold,italic

SelStrikeThru

Some unique methods of the rich textbox are:

LoadFile- Open a file and load the contents into the control.

SaveFile- Save the control contents into a file.