picture

How to Make Modal Form?

Just a short notice how to make modal forms in Delphi. Assume, you have two Forms: AForm and BForm. AForm is your main form and you want now to call B as modal form (a modal form is a child form that requires the user to interact with it before it gets possible to return to the parent form). So inside your AForm you call BForm in the following way:


procedure AForm.Button1Click(Sender: TObject);
var b : BForm;
begin
  b := BForm.Create(nil);
  try
    if b.ShowModal = mrOk then begin
      // your code if OK Button is clicked
    end
    else begin
      // your code, if cancel is clicked
    end;
  finally
      b.Release;
  end;
end;

On your BForm you need two Buttons. One of them you use for Ok and the other one for Cancel. The only thing you need to do is to chage respectively the ModalResult property of these buttons as shown in the picture bellow:

Change properties of the buttons for modal result

That’s all. Notice, that the value of the Visible-property of your BForm should be false.

Leave a Reply

You must be logged in to post a comment.