Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi onshow main form / modal form

I have a project which has a main form and some other forms. When the app loads it needs to carry out some tasks and show the results in a modal form on top of the main form. The problem I have is that if I put the call to the function to do the tasks / creates and Show the modal form in the main forms onshow event the modal form appears but the main form does not until the modal form is closed, which is what I would expect to happen. To counter this I have added a timer to the main form and start it on the main forms onshow event the timer calls the function to do the tasks / create and show the modal form. So now the main form appears before the modal form.

However I cannot see this being the best solution and was wondering if anyone could offer a better one.

I am using Delphi 7

Colin

like image 950
colin Avatar asked Jan 19 '12 13:01

colin


People also ask

What is modal form in Delphi?

Delphi supplies modal forms with the ModalResult property, which we can read to tell how the user exited the form. The following code returns a result, but the calling routine ignores it: var F:TForm2; begin F := TForm2. Create(nil); F. ShowModal; F.

How do I open a new form in Delphi?

Select the File > New > Form from the main menu to display the new form. Remove the form from the Auto-create forms list of the Project > Options > Forms page. This removes the form's invocation at startup.


2 Answers

One commonly used option is to post yourself a message in the form's OnShow. Like this:

const
  WM_SHOWMYOTHERFORM = WM_USER + 0;

type
  TMyMainForm = class(TForm)
    procedure FormShow(Sender: TObject);
  protected
    procedure WMShowMyOtherForm(var Message: TMessage); message WM_SHOWMYOTHERFORM;
  end;

...


procedure TMyMainForm.FormShow(Sender: TObject);
begin
  PostMessage(Handle, WM_SHOWMYOTHERFORM, 0, 0);
end;

procedure TMyMainForm.WMShowMyOtherForm(var Message: TMessage);
begin
  inherited;
  with TMyOtherForm.Create(nil) do begin
    try
      ShowModal;
    finally
      Free;
    end;
  end;
end;
like image 116
David Heffernan Avatar answered Nov 02 '22 02:11

David Heffernan


Why dont you use the MainForm OnActivate event like so?

procedure TMyMainForm.FormActivate(Sender: TObject);
begin
  //Only execute this event once ...
  OnActivate := nil;

  //and then using the code David Heffernan offered ...
  with TMyOtherForm.Create(nil) do begin
    try
      ShowModal;
    finally
      Free;
    end;
end;

Setting the event to nil will ensure that this code is only run once, at startup.

like image 2
Pieter van Wyk Avatar answered Nov 02 '22 03:11

Pieter van Wyk