There are two cases at all. First, more simple, when a form is
created just before showing and destroyed immediately after finishing
modal dialog. For such case, it is not necessary to have separate Applet
object (for MCK project, component TKOLApplet is not required).
// showing of a modal dialog, created just before
// starting it and destroyed after finishing it
procedure TForm1.Button1Click(Sender: PObj);
begin
NewForm2( Form2, Applet );
// insert here any code to make
// changes before showing Form2
Form2.Form.ShowModal;
// insert here any code to read
// some data from Form2
Form2.Form.Free; // not Close !
//ShowMessage( 'End of TForm1.Button1Click' );
end;
For such case it is sufficient to create an event handler (like
following), which is called (e.g.) in result of click on some button
(but for this case, even such handler is not needed, and form can be
closed clicking 'x' button of window caption or by some another way
(e.g. system menu).
procedure TForm2.Button1Click(Sender: PObj);
begin
Form.ModalResult := 1; // any value <> 0
end;
The second case is harder a bit. If the form is already existing (but
usually is hidden), and we want to show it modal, but without destroying
it immediately after finishing the dialog (suppose, we want to hide it
instead). For such case, separate object must be used as Applet (for MCK
project this means, that TKOLApplet object should be used).
// Showing a dialog modal:
procedure TForm1.Button1Click(Sender: PObj);
begin
Form2.Form.ShowModal;
Form2.Form.Hide;
end;
// Finishing a dialog on button click:
procedure TForm2.Button1Click(Sender: PObj);
begin
Form.ModalResult := 1; // any value <> 0
end;
// Event handler for form's event OnClose
// must be defined to prevent form destroying:
procedure TForm2.KOLForm1Close(Sender: PObj;
var Accept: Boolean);
begin
Accept := FALSE; // prevents closing
Form.ModalResult := -1; // any value <> 0
end;
Take in your attantion, that in both cases final steps are made by the
code, which is initiating showing modal dialog, i.e. in Form1 code. |