Common Dialog Controls

The Windows operating system provides several standard dialog boxes that are used by most applications to enable the user to perform standard tasks. These common dialog boxes are encapsulated into several VCL components. The most often used common dialog boxes are TColorDialog, TFindDialog (discussed in Chapter 16), TFontDialog, TOpenDialog, and TSaveDialog. All these components are nonvisual at design time, but they appear onscreen at run time after a call to the Execute method. If you want to see what a common dialog control looks like at run time, right-click the component on the Designer Surface and select Test Dialog from the context menu.

image from book
Figure 15-14: Testing a common dialog control at design time

The TColorDialog Component

The TColorDialog component enables the user to select a basic or custom color. The two most often used properties of the TColorDialog component are the Color and Options properties. The Color property contains the selected color, and the Options property enables you to customize the dialog box. For instance, if you include cdFullOpen in the Options property, the dialog box automatically displays not only the basic set of colors but also the section of the dialog box that enables you to select custom colors, as shown in Figure 15-15.

image from book
Figure 15-15: The TColorDialog component

To display a common dialog box onscreen, you have to use the Execute method. The Execute method is actually a function that returns True if the user clicks OK on the dialog box and False if the user closes the dialog box in any other manner. The following code shows how to change the background color of the form using the TColorDialog component.

Listing 15-2: Changing the background color of the form

image from book
procedure TForm1.Button1Click(Sender: TObject); begin   { select current color}   ColorDialog1.Color := Color;   { display and optionally change the color }   if ColorDialog1.Execute then     Color := ColorDialog1.Color; end;
image from book

The TFontDialog Component

The TFontDialog component enables the user to select a font and define the font's style.

image from book
Figure 15-16: The TFontDialog component

In most cases, only the Font property of the TFontDialog component is used. Again, to display the Font dialog, call its Execute method; if the user clicks OK, assign the selected font to the target control (see Listing 15-3).

Listing 15-3: Modifying the Font property of a TLabel component

image from book
procedure TForm1.Button2Click(Sender: TObject); begin   FontDialog1.Font.Assign(Label1.Font);   if FontDialog1.Execute then     Label1.Font.Assign(FontDialog1.Font); end;
image from book

The TOpenDialog and TSaveDialog Components

The TOpenDialog and TSaveDialog components enable you to select a file. The TOpenDialog component is suited for opening existing files, whereas the TSaveDialog is suited for creating (saving) new files. Figure 15-17 displays a TOpenDialog component.

image from book
Figure 15-17: The TOpenDialog component

The most important properties of both components are the Filter, FileName, FilterIndex, and DefaultExt properties. The Filter property defines which file types are displayed in both dialog boxes. You can define the file filter manually or by using the Filter Editor (see Figure 15-18). The Filter Name column usually contains the file type description, and the Filter column contains one or more extensions. If more than one extension is associated with a particular file type description, separate the extensions with a semicolon.

image from book
Figure 15-18: The Filter Editor

If you want to define the filter manually, use the vertical bar character. Here is a filter string that can be used to filter text and rich text documents:

Text Documents (*.txt)|*.txt|RichText Documents (*.rtf)|*.rtf

The FilterIndex property is very simple: It defines which filter is selected by default when the dialog box is displayed. The DefaultExt string property is only important in the TSaveDialog component. It defines the default extension that should be appended to file names. If you forget to define a default extension, the files saved by your application will have no extension. For instance, if you're creating a text editor, you should set the DefaultExt property to "txt" (without the quotes). This way, all documents saved by your application will have the necessary .txt extension.

Inside Delphi 2006
Inside Delphi 2006 (Wordware Delphi Developers Library)
ISBN: 1598220039
EAN: 2147483647
Year: 2004
Pages: 212
Authors: Ivan Hladni

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: