Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Pascal
Expertise: Beginner
May 9, 1997

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 Dialogs palette: They're not real Delphi windows. In fact, they're all created from standard 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 two buttons 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 the main 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 a Palette 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 it below):
    unit dlgComp;
    
    interface
    
    uses
      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;
    
    implementation
    
    uses 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 and set some properties on it. Study and use it as a starting template for your own stuff. The important thing to remember is that before you show the form, you must set the component properties on the form that are subject to 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 the result of Execute. That ensures that the resources taken up by the form are freed up after it has displayed, and by setting the form variable to nil, 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 to know that it was shown modally and destroys the form's display. But the form is not destroyed completely. So as I explained above, you have to do it yourself.

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

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date