Dialogs: Implementing your own execute method

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:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as