Modal Form OnShow Event

I have a Delphi 4 app which shows a modal form from my main form when a button is clicked. I am having a problem with the modal form's OnShow event. Using debugging I found that the modal form's OnShow event only gets called the first time I call the ShowModal method. On all subsequent calls, the OnShow event is not triggered allthough the OnActivate event is triggered. Any suggestions on what might be causing this ?
-Thanks
-Mike
mferdockAsked:
Who is Participating?
 
karooConnect With a Mentor Commented:
Mike,

weird problem,

hmmm, try the following:

private
  procedure MyFormShow(Sender: TObject);
...

procedure TForm1.FormCreate(Sender: TObject);
begin
  OnShow := MyFormShow;
end;

procedure TForm1.MyFormShow(Sender: TObject);
begin
  //your OnShow code here
end;

Regrads
Ben;-)
0
 
intheCommented:
do you call form2.hide before try to call onshow the subsequent times?

0
 
rwilson032697Commented:
This will happen is the form has not been closed once you are finished with it, or you are somehow calling ShowModal more than once to invoke the form.

Once the form is shown then subsequent calls to ShowModal will not call the OnShow event.

Cheers,

Raymond.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
intheCommented:
from delphi help:
->
OnShow Occurs when the form is shown (that is, when its Visible property is set to True).
<-

so on your form2 onclose event call form2.visible := false;
then when form1 calls the form2.showmodal again the onshow event will be fired.

0
 
geobulCommented:
Hi Mike,

You can solve your problem in the calling form this way:

procedure TForm1.Button1Click(Sender: TObject);
begin
  Form2.ShowModal;
  Form2.Hide;
end;

if you do not want to change the form2.

Regards, Geo
0
 
mferdockAuthor Commented:
I have tried calling the Hide method before the ShowModal method and also after the ShowModal method but with no success. I use SendMessage to send a WM_CLOSE message to the modal form to close it once I am done with the form. When I use the debugger to check the visible property for the modal form before calling the ShowModal method, the property is False.
I could swear that it use to work before I installed Delphi 4 update pack 3 on my PC, but since then the ONShow event does not appear to be working correctly. Anyone know of any problems with update pack 3 that might cause this ? Any other suggestions ?
-Mike
0
 
geobulCommented:
Hi Mike,

Possible solution is to create the modal form every time you click the button and free after (or on) closing it. If it's not a problem to your project, of course.

procedure TForm1.Button1Click(Sender: TObject);
begin
  Application.CreateForm(TForm2, Form2); { this line is removed from your project unit }
  Form2.ShowModal;
  Form2.Release;
end;

or:
procedure TForm1.Button1Click(Sender: TObject);
begin
  Application.CreateForm(TForm2, Form2); { this line is removed from your project unit }
  Form2.ShowModal;
end;

{ Modal form OnClose event }
procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

Regards, Geo
0
 
mferdockAuthor Commented:
I am not sure why assigning your own event handler to the form's OnShow event handler would work, but I tried this and it does work. If any one else has any comments on why this works feel free to chime in.
Thanks Ben...

-Mike
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.