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
One commonly used option is to post yourself a message in the form's
OnShow
. Like this:The
OnShow
event is fired immediately before the call to the Windows API functionShowWindow
is made. It is this call toShowWindow
that actually results in the window appearing on the screen.So you ideally need something to run immediately after the call to
ShowWindow
. It turns out that the VCL code that drives all this is inside aTCustomForm
message handler forCM_SHOWINGCHANGED
. That message handler fires theOnShow
event and then callsShowWindow
. So an excellent solution is to show your modal form immediately after the handler forCM_SHOWINGCHANGED
runs. Like this:Why dont you use the
MainForm OnActivate
event like so?Setting the event to nil will ensure that this code is only run once, at startup.