devxlogo

Dialogs: Implementing your own execute method

Dialogs: Implementing your own execute method

Question:
I use Delphi 1.0. The standard dialogs (such as TOpenDialog and TPrintDialog) offer a method called execute. It executes the form/dialog, and waits until it returns. I would like to implement a function like this myself. I need to call a form from a procedure, and want that procedure to wait until the user presses OK, Cancel or whatever in the new form, just like the Execute method does for the built-in dialogs. I tried it by using the following code:

form2.visible := true;while form2.visible do ;{ whatever has to be done afterwards… }
but this only starts an eternal loop, after which I have to reboot my PC!

Answer:
Here’s one thing you should understand about the dialogs in the Dialogspalette: They’re not real Delphi windows. In fact, they’re all created fromstandard Windows API calls and assigned the class TForm when created. But about your particular problem, here’s what to do:

  1. Create a form that will be used as the dialog box, and drop twobuttons on it. Their labels should read OK and Cancel, respectively.
  2. Set the OK button’s ModalResult property to mrOK
  3. Set the Cancel button’s ModalResult property to mrCancel
  4. Now you need to create a component. Click on Component|New from themain menu, then name the component something like TMyDialog (or whatever).
  5. In the Ancestor type field, choose “TComponent” from the list.
  6. In the Palette entry field, type in any name that you want to use as aPalette entry. For my own personal components, I always use my initials, “BD.”
  7. Then write code that’s similar to the following listing (we’ll discuss itbelow):
    unit dlgComp;interfaceuses  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;type  TMyDialog = class(TComponent)  private    { Private declarations }    FCaption,    FMessage : String;    FMsgFont : TFont;    procedure SetFCaption(Value : String);    procedure SetFMessage(Value : String);  protected    { Protected declarations }  public    function Execute : Boolean;  published    property Caption : String read FCaption write SetFCaption;    property Message : String read FMessage write SetFMessage;  end;procedure Register;implementationuses dlgForm;var  frmDlg : TTestDialog;function TMyDialog.Execute : Boolean;begin  frmDlg := TTestDialog.Create(Application);  with frmDlg do begin    lblMsg.Caption := Self.Message;    lblMsg.Update;    frmDlg.Caption := Self.Caption;    Invalidate;    ShowModal;    Result := ModalResult = mrOK;    Free;  end;  frmDlg := nil; //really destroy it.end;procedure TMyDialog.SetFCaption(Value : String);begin  if FCaption  Value then    FCaption := Value;end;procedure TMyDialog.SetFMessage(Value : String);begin  if FMessage  Value then    FMessage := Value;end;procedure Register;begin  RegisterComponents(‘BD’, [TMyDialog]);end;end.
    The code above is a simple listing that shows you how to invoke a form andset some properties on it. Study and use it as a starting template foryour own stuff. The important thing to remember is that before you showthe form, you must set the component properties on the form that are subjectto change to the property values entered by the user. I have done this in the Execute method of the component with the following code:
      frmDlg := TTestDialog.Create(Application);  with frmDlg do begin    lblMsg.Caption := Self.Message;    lblMsg.Update;    frmDlg.Caption := Self.Caption;    Invalidate;    ShowModal;    Result := ModalResult = mrOK;    Free;  end;  frmDlg := nil; //really destroy it.
    Notice in the code above that I immediately Free the form after setting theresult of Execute. That ensures that the resources taken up by the formare freed up after it has displayed, and by setting the form variable tonil, I make sure the pointer variable is completely emptied of any value.

    The form I call up is a simple one with a couple of buttons and a label to display a message.When the form returns one of its modal results, Windows is smart enough toknow that it was shown modally and destroys the form’s display. But the formis not destroyed completely. So as I explained above, you have to do ityourself.

    With this component template, you have thebeginnings of a dialog component that runs similarly to thedialog components in Delphi.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist