| your own Dialog Form | ![]() |
|
Introduction Dialog forms are forms, that pop-up at runtime to request information from the user. After this information is supplied the user clicks an OK or a CANCEL button to resume operation. Delphi has ready-to-use dialogs for openening- and saving files, selecting fonts etc. Until recently I constructed my own dialog forms using statements as mydialogForm.show and suspending operation by application.processmessages or supplying a procedure to be called after closing of the dialogForm. Code was added to disable/enable the calling form. There is an easier way. the Recipe Add button1 with caption 'dialog' to form1. Add label1 to this form. The code of unit1: |
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses Unit2;
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
if form2.execute then label1.caption := 'OK'
else label1.caption := 'cancel';
end;
end.
|
Add Form2 (the dialog form). Add button1 with caption 'OK' and set modalresult to mrOK in the object inspector. Add button2 with caption 'CANCEL' and set modalresult to mrCancel. Add a function execute to TForm2. The code for unit2: |
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm2 = class(TForm)
Button1: TButton;
Button2: TButton;
private
{ Private declarations }
public
function execute : boolean;
end;
var
Form2: TForm2;
implementation
{$R *.DFM}
function TForm2.execute : boolean;
begin
showmodal;
if modalResult = mrOK then result := true else result := false;
end;
end.
|
That's all. Processing is suspended after the showModal statement in unit2, form2 pops up and form1 is disabled. Pressing button1 or button2 sets the modalresult of form2 <> 0, processing resumes, form2 closes and form1 is enabled. Any processing needed on form2 may be started by the onShow event. |