The main form is not displayed when showing the modal form in the main OnShow form?

I created one application in which the main form is called on the Form on the FormShow event of the main form. Subformation is displayed and gives two choices. If the option "First option" is selected on an additional form, then a message is displayed, after which the main form is displayed. Now, when the application is launched for the first time, after selecting the option selected in the Meassage subformat. But I want to display a message with the main form as the background. Therefore, 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; 
+1
source share
5 answers

If I understand correctly, then your problem is that when the message box appears, your main form is still invisible.

If so, you have two options:

  • Do not show your SubForm from the SubForm event of the main form, but later
  • Do not show message immediately after ShowModal returns, but later

For point number 2, you can use a similar approach, as I suggested here , using PostMessage . So your code will look something 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 WM_SHOWMYDIALOG handler then displays the actual message. This approach may also work for point 1, see ain answer .

PostMessage sends a message to the message queue of your application, which will be processed after the main form becomes visible.

+2
source

Another option is to use OnActivate in the main format instead of onShow.

+2
source

If I understand you correctly, you want

 const UM_AFTERSHOW = WM_APP + 1; type TForm1 = class(TForm) protected procedure UMAfterShow(var Msg: TMessage); message UM_AFTERSHOW; procedure DoShow; override; end; procedure TForm1.DoShow; begin inherited; PostMessage(Self.Handle, UM_AFTERSHOW, 0, 0); end; procedure TForm1.UMAfterShow(var Msg: TMessage); begin ShowMessage('Enter the value'); end; 

By showing your message in the UMAfterShow handler, you give the main form the opportunity to become visible and, therefore, be in the background.

+1
source

The problem you see (if I understand correctly) is that FormShow is called before your main form is visible. In this way, a message dialog is displayed in front of your main form.

What you need to do is use PostMessage to send a message to your main form, which you then process. This will complete your FormShow code, and the code will run after the form is displayed.

Look here for an example.

0
source

Another option would be to drop the TTimer component in your main form to trigger a message dialog.

Drop the TTimer component in the main form and set the included property to False and change the time from 1000 to 100. Compose a dialog box for your message and set the Timer.Enabled property to False in the timer event to avoid repeated operations.

Now you can enable the timer in the place where you would indicate the message dialog box in your main OnShow event of the form.

0
source

All Articles