Search code examples
delphimodal-dialogtform

Main Form not displaying when showing modal form in main form's OnShow?


I have created one application in which Main Form Calls Sub Form on FormShow event of Main Form. Sub Form is displayed and gives two options to choose. If First option on sub form is selected then then a Message is displayed and after that Main form will be displayed. Now when application runs on first time then after option selected on subform Meassage will be displayed. But i want to display message with Main Form as Background. So any solution to this. below is the FormShow code.

Procedure TMainForm.FormShow(Sender:TObject);
begin
  if (SubForm.ShowModal = mrOK) and bOption1 then
  begin
    ShowMessage('Enter the value');
  end;
end;

Solution

  • If I understand correctly then your problem is that when the message box show up your main form is still invisible.

    If this is the case then you have two options:

    1. Don't show your SubForm from the OnShow event of the main form, but at a later time
    2. Don't show the message directly after ShowModal returns, but at a later time

    For point number 2 you can use a similar approach as I suggested here, using PostMessage. So your code would look somethind like this:

    procedure TMainForm.FormShow(Sender:TObject);
    begin
      if (SubForm.ShowModal = mrOK) and bOption1 then
      begin
        PostMessage(Self.Handle, WM_SHOWMYDIALOG, 0, 0);
      end;
    end;
    

    The handler of WM_SHOWMYDIALOG then displays the actual message. This approach can also work for point 1, see ain's answer.

    PostMessageposts a message to your application's message queue which will be processed after the main form finished becoming visible.