Modeless Dialog

The second example of dialog boxes shows a more complex modal dialog box that uses the standard approach as well as a modeless dialog box. The main form of the DlgApply example (and of the identical CLX-based QDlgApply demo) has five labels with names, as you can see in Figure 9.14 and by viewing the source code on the companion CD.

FIGURE 9.14:

The three forms (a main form and two dialog boxes) of the DlgApply example at run time

FIGURE 9.14:

The three forms (a main form and two dialog boxes) of the DlgApply example at run time

If the user clicks a name, its color turns to red; if the user double-clicks it, the program displays a modal dialog box with a list of names to choose from. If the user clicks the Style button, a modeless dialog box appears, allowing the user to change the font style of the main form's labels. The five labels of the main form are connected to two methods, one for the OnClick event and the second for the OnDoubleClick event. The first method turns the last label a user has clicked to red, resetting to black all the others (which have the Tag property set to 1, as a sort of group index). Notice that the same method is associated with all of the labels:

procedure TForm1.LabelClick(Sender: TObject); var

I: Integer; begin for I := 0 to ComponentCount - 1 do if (Components[I] is TLabel) and (Components[I].Tag = 1) then TLabel (Components[I]).Font.Color := clBlack; // set the color of the clicked label to red (Sender as TLabel).Font.Color := clRed; end;

The second method common to all of the labels is the handler of the OnDoubleClick event. The LabelDoubleClickmethod selects the Caption of the current label (indicated by the Sender parameter) in the list box of the dialog and then shows the modal dialog box. If the user closes the dialog box by clicking OK and an item of the list is selected, the selection is copied back to the label's caption:

procedure TForm1.LabelDoubleClick(Sender: TObject); begin with ListDial.Listbox! do begin

// select the current name in the list box ItemIndex := Items.IndexOf (Sender as TLabel).Caption); // show the modal dialog box, checking the return value if (ListDial.ShowModal = mrOk) and (ItemIndex >= 0) then // copy the selected item to the label (Sender as TLabel).Caption := Items [ItemIndex];

Notice that all the code used to customize the modal dialog box is in the LabelDoubleClick method of the main form. The form of this dialog box has no added code.

The modeless dialog box, by contrast, has a lot of coding behind it. The main form simply displays the dialog box when the Style button is clicked (notice that the button caption ends with three dots to indicate that it leads to a dialog box), by calling its Show method. You can see the dialog box running in Figure 9.14 above.

Two buttons, Apply and Close, replace the OK and Cancel buttons in a modeless dialog box. (The fastest way to obtain these buttons is to select the bkOK or bkCancel value for the Kind property and then edit the Caption.) At times, you may see a Cancel button that works as a Close button, but the OK button in a modeless dialog box usually has no meaning. Instead, there might be one or more buttons that perform specific actions on the main window, such as Apply, Change Style, Replace, Delete, and so on.

If the user clicks one of the check boxes of this modeless dialog box, the style of the sample label's text at the bottom changes accordingly. You accomplish this by adding or removing the specific flag that indicates the style, as in the following OnClick event handler:

procedure TStyleDial.ItalicCheckBoxClick(Sender: TObject);

begin if ItalicCheckBox.Checked then

LabelSample.Font.Style := LabelSample.Font.Style + [fsItalic]

else

LabelSample.Font.Style := LabelSample.Font.Style - [fsItalic];

end;

When the user selects the Apply button, the program copies the style of the sample label to each of the form's labels, rather than considering the values of the check boxes:

procedure TStyleDial.ApplyBitBtnClick(Sender: TObject);

begin

  1. Label1.Font.Style := LabelSample.Font.Style;
  2. Label2.Font.Style := LabelSample.Font.Style;

As an alternative, instead of referring to each label directly, you can look for it by calling the FindComponent method of the form, passing the label name as a parameter, and then casting the result to the TLabel type. The advantage of this approach is that we can create the names of the various labels with a for loop:

procedure TStyleDial.ApplyBitBtnClick(Sender: TObject);

I: Integer;

(Form1.FindComponent ('Label' + IntToStr (I)) as TLabel).Font.Style := LabelSample.Font.Style;

end;

TIP The ApplyBitBtnClick method could also be written by scanning the Controls array in a loop, as I've already done in other examples. I decided to use the FindComponent method, instead, to demonstrate a different technique.

This second version of the code is certainly slower, because it has more operations to do, but you won't notice the difference, because it is very fast anyway. Of course, this second approach is also more flexible; if you add a new label, you only need to fix the higher limit of the for loop, provided all the labels have consecutive numbers. Notice that when the user clicks the Apply button, the dialog box does not close. Only the Close button has this effect. Consider also that this dialog box needs no initialization code because the form is not destroyed, and its components maintain their status each time the dialog box is displayed.

Was this article helpful?

0 0
Self Publishing a Book The Easy Way

Self Publishing a Book The Easy Way

This comprehensive guide will present you with a variety of self-publishing options and explore their viability. Well take a look at all types of books and book-delivery systems.

Get My Free Ebook


Post a comment