Dialogs: Implementing your own execute method

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!

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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.