Designing Dialog Boxes


Windows applications very often use dialog boxes to communicate with the user. You can either use one of the common dialogs or create your own. Common dialogs are described later in the book.

A dialog box is nothing more than a customized form. If you want to have a non-resizable dialog box without an icon on the title bar, you only have to set the BorderStyle property to bsDialog.

image from book
Figure 12-20: A bsDialog dialog box

If you want to have a dialog box that more closely resembles, for instance, the Project Options dialog box that can be resized or has an icon in the title bar, you have to do the following:

  1. Set the BorderIcons property to [biSystemMenu].

  2. Set the BorderStyle property to bsSizeable or bsSingle.

  3. Optionally, set the Position property to poScreenCenter to position the form in the center of the screen.

Dialog boxes are usually modal forms that are used to retrieve additional information from the user. Dialog boxes must provide the user at least two buttons: OK and Cancel. The OK button is used to confirm the work in the dialog box is done, and the Cancel button is used to disregard the work done in the dialog box. Both buttons have to close the dialog box.

Now, let's create a dialog box that will enable the user to change the color of the main form. Add a TLabel component and two TButton components and modify them to resemble the ones displayed in Figure 12-21.

image from book
Figure 12-21: A simple dialog box

Now we have to modify the ModalResult property of the OK and Cancel buttons. Set the ModalResult property of the OK button to mrOK and the ModalResult property of the Cancel button to mrCancel. The ModalResult property enables us to do two things at the same time: close the dialog box and notify the caller whether the user selected OK, Cancel, or something else. When the user clicks a button with a modified ModalResult property, the value of the ModalResult property is automatically assigned to the ModalResult property of the dialog box. So, to see what button the user selected, we have to test the ModalResult property of the dialog box.

The following code shows how to use this dialog box to enable the user to modify the color of the main form.

Listing 12-19: Using the custom dialog box

image from book
procedure TForm1.Button1Click(Sender: TObject); begin   Form2.ShowModal;   if Form2.ModalResult = mrOK then   begin     Caption := 'User selected OK.';     Color := clWhite;   end; end;
image from book

To be technically and professionally correct, we have to do two more things. First, we have to enable the user to cancel the dialog box with the Esc key and to confirm the dialog box with the Enter key. Then, we have to remove the dialog box from the auto-create list and dynamically create it when necessary.

To fire the Cancel button's OnClick event when the user presses Esc on the keyboard, you have to set the button's Cancel property to True. To fire the OK button's OnClick event when the user presses Enter on the keyboard, set the button's Default property to True, and you're done.

Listing 12-20: Dynamically creating the dialog box

image from book
procedure TForm1.Button1Click(Sender: TObject); begin   Form2 := TForm2.Create(Self);   Form2.ShowModal;   if Form2.ModalResult = mrOK then   begin     Caption := 'User selected OK.';     Color := clWhite;   end;   Form2.Free; end;
image from book

image from book
Figure 12-22: The finished application



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

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net