Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Re-Open Form

Status
Not open for further replies.

jfreak53

IS-IT--Management
Apr 30, 2004
44
GT
How do I re-open a form after I have closed it with the release command? I have a second form on an app that I close with release but there are times I need to reopen it?

Thanks in advance
 
Release does a full destroy on all claimed resources and allocated memory. You have to newly instantiate the form to be able display it again.
An easier way would be to close, and later show the form again, but then any field contents (if not cleared explcitly) would still be available.

HTH
TonHu
 
My problem is that when the computer shuts down this program that is in the sys tray wont close once I show the second form for the first time. So what I was thinking was if I destroy it every time I close it instead of using close then the program will shutdown fine. I can't call the wm_query command for shutdown message, for some reason the program isn't getting the message. Any other way of doing this that you guys know of. How would I instantiate the form again? I don't really care about the fields.
 
If I'm controlling the creation of my forms (vs. having them
automatically created) I use code similar to what follows. Now when you need to open the form you call your function to create the new instance of the form. If you don't .ShowModal then the code below will show the form and then try to release it. In that case have the form clean up after itself.
Code:
procedure OpenMyForm;
var
  Options: TfrmOptions;
begin
  Options := TfrmOptions.Create(Application); //could also be nil
  try
    Options.LoadMyData;
    Options.ShowModal;
  finally
    Options.Release;
  end;
end;
 
You shouldn't be calling Release if it is an auto-created form. If Close is used for secondary forms, Show or ShowModal will re-open the form.

If Form1 opens Form2 with Show, Form1 or Form2 can close Form2 with Close or Hide.

If Form1 opens Form2 with ShowModal, Form1 can NOT close Form2. Form2 must call Close.

If Form1 opens Form2 with ShowModal, Form2 can call Hide, but your App will HANG.

HTH

Roo
Delphi Rules!
 
Note: My reply was to the first post only. (Did not see posts 2, 3 or 4 until after)

Roo
Delphi Rules!
 
Response to: DjangMan

You see I tried that before also. I commented out the Application.CreateForm(TForm2, Form2) in the project source then I added that. I had tried that before and all I get is the error when I try to open the form:

Project raised exception class EAccessViolation with message 'Access violation at address 0045A956 in module ...'.

Then it stops process and won't open form if I continue run. So that's why I posted here I thought I was doing something wrong somewhere.

And to answer the other answers and questions, I know that's not the correct way to do things, you show or showmodal, but this is a special case, I need to do it this way because of shutdown. Unless you have another solution.
 
I think you just want to .Close. If your form is asking the user to do something when it is closing then you'll need to change that.

If you don't want a form to be auto-created you change that in the Project, Options... dialog.

Also - you can change the scope of the form variable to be a variable of the main form. Then you can .release it when the main form closes instead of when the user make it not visible anymore.
 
Ok I am allowing the application to autocreate the form. That is fine, if not then there is an exception when I try to create. The problem is that even though I'm using the code so that the form is released when it's done when I close the form (the application is still open) the system won't shut down. BUT...If I open the app, but never open form2 and and close it then the system shuts down just fine and closes the app. What is going on here? How do I stop this and let the program shut down when the system shut down. Note: there is no messages waiting for user, plus I am releasing the form before I try to shutdown, so there is nothing in it.

Any ideas?
 
What exact code do you need to see? The tray icon code? The code I'm using to create the form is the one you posted. Another problem just came up. If I am having the form auto created and using the code to create the form then it pop it up but I cannot use any of the controls on it. Second if I don't auto create form then it will popup but I can never close it, it raises and exception, it also raises one when I popup the form.

 
Ok never mind I just figured it out. For some reason my first form wasn't capturin the "WMQueryEndSession" so I have never used it. But up until now I haven't needed to, I use it in ever other app I have that sits in tray to make sure it shuts down correctly, but this one didn't have it, but it was working without it. But it stopped working and closing on shutdown with this new form I added. But for some odd reason I won't even try to speculate it does work for Form2! So all I did was keep my form using show and close and I just put the "WMQueryEndSession" in form2 to make sure it shuts down right. Now all is working just fine, don't know why but it is.

Thanks for everyones help.
 
One general remark.

Do NOT use .Release but rather .Free. The end result is the same (form will be freed) but Release is the async version of .Free and can get you into trouble when the message pump (wndproc) no longer exists.

see this thread for more info:
thread102-1438941

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
jfreak53:

does your form2 has OnCloseQuery event?

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
whosrdaddy:

Yes, but only now that I added the WM_QUERY procedure. Before it didn't have it. I also tried adding just a simple Application.Terminate on the CloseQuery and it didn't work. But now it works fine that I added the WM_QUERY which wasn't working before I added this second form. The program is a little out of wack for some reason. But it all works now.
 
some background info:

WMQueryEndSession is the message is been send to an application when it has closed by user or by OS.

in fact delphi implements this message and calls the OnCloseQueryEvent.

this is from the Forms unit:

Code:
procedure TCustomForm.WMQueryEndSession(var Message: TWMQueryEndSession);
begin
  Message.Result := Integer(CloseQuery);
end;

what you really need is implementing WM_ENDSESSION (which is sent when windows shuts down).
All you need to call in this procedure is Halt.
Halt will ensure that all destructors are called (so you open files will be closed and cleaned up)

Code:
procedure TForm1.FormCreate (Sender: TObject); 
begin 
  Application.HookMainWindow (HookProc)
end; 

function TForm1.HookProc (var message: TMessage) : Boolean; 
begin 
  result := False; 
  if message.Msg = WM_ENDSESSION then 
  begin 
    if WordBool (message.WPARAM) then 
    begin 
      { Windows is closing down - clean up!! } 
      
      { This should execute all ExitProcs, close windows and call destructors.. }
      Halt
     
      { This should close things down properly,                                 }
      { but do we have enough time to handle any posted messages before Windows }
      { is already down??  This will result in a PostQuitMessage that might     }
      { never arrive!                                                           } 
      { This doesn't always work - avoid it } 
      {      Close;}      
    end
  end
end;

Add this code to your main form.

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top