Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Closing a form programatically?

1,528 views
Skip to first unread message

Fred VonBerg

unread,
Feb 22, 1999, 3:00:00 AM2/22/99
to
I need some way of closing a form under program control, then return to
the MAIN form. I have gone through my 2 foot stack of Delphi books to
no avail.
Any ideas?

I am opening the form as follows:

procedure TScnTest.Stkupd;
var form:TScnMsUpd;
begin form:=TScnMsUpd.create(self);form.ShowModal;form.free;end;


Does anyone have any ideas?
Close does not work unless it is inside a button procedure, EndThread
closes everything, as does EndProcess. STUMPED in Chicago.


Brian [borland.com]

unread,
Feb 22, 1999, 3:00:00 AM2/22/99
to
Hi Fred,

Take a look at the OnClose event for your form- try assigning caFree to the
Action. Then call Close. Also, wrap your code in a try.. finally block.

Brian
borland.com

Marian Aldenhövel

unread,
Feb 23, 1999, 3:00:00 AM2/23/99
to
Hi,

>begin form:=TScnMsUpd.create(self);form.ShowModal;form.free;end;

You are showing modally. Assigning non-zero to ModalResult should be
sufficient. At least that's how it's documented and how it works here.

Throw away those books.

Ciao, MM

--
Marian Aldenhövel, Hainstraße 8, 53121 Bonn, Germany
Tel: +49 228 624013 Fax: +49 228 624031
http://www.gammasoft.de/maier
"Was schiefgehen kann geht schief"

Fred VonBerg

unread,
Feb 23, 1999, 3:00:00 AM2/23/99
to
I did as you suggested to no avail. .

My code in form 2 is extrememly simple:
On Form2Activate
begin
procedure 1: stuffs some strings into several panel.captions
procedure 2: process some text files + display progress in
panel.captions
close; where I want to close the form programatically
end;
Closing the form by using a button or menu works, but I don't want any user
interactions:. the goal is to just close the form unser program control when
file processing is finished and then return to the main form.

Fred VonBerg

unread,
Feb 23, 1999, 3:00:00 AM2/23/99
to Marian Aldenhövel
I tried your idea to no avail. Is there a special way or place to set
ModelResult?

My code in form 2 is extrememly simple:

On Form2Activate
begin
procedure 1: //stuffs some strings into several
panel.captions
procedure 2: //process text files, display progress in
panel.captions
modalresult := 3; // where I want to close the form
programatically
end;

I also tried
ModalResult:=mrCancel
also MrCancel to no avail.

Peter Below (TeamB)

unread,
Feb 23, 1999, 3:00:00 AM2/23/99
to
> I tried your idea to no avail. Is there a special way or place to set
> ModelResult?
>
> My code in form 2 is extrememly simple:
>
> On Form2Activate

Fred, this the root of your problem: you do all this in the forms
OnActivate handler which is executed *before* the modal message loop in
ShowMessage starts to run. There is an assignment ModalResult := mrNone
before the start of this loop and that wipes out your efforts to close the
form.

Replace the assignment to ModalResult with this:

PostMessage( handle, wm_syscommand, sc_close, 0 );

Peter Below (TeamB) 10011...@compuserve.com)
No e-mail responses, please, unless explicitely requested!


Fred VonBerg

unread,
Feb 23, 1999, 3:00:00 AM2/23/99
to
THAT DID IT!
You are terrific. I would never have figured that one out.

Should I have done those routines in another handler, such as maybe
OnForm2Display?

Peter Below (TeamB)

unread,
Feb 24, 1999, 3:00:00 AM2/24/99
to
In article <36D343B1...@worldnet.att.net>, Fred VonBerg wrote:
> Should I have done those routines in another handler, such as maybe
> OnForm2Display?
>

Whatever of the form events you use, with one exception (OnPaint) will
execute before the modal message loop starts to run and will have the
same problem. For a task like yours I usually either post a user
message to the form in its onCreate handler and do the processing in
the handler for this message, or use the OnPaint event, like this:

Procedure TFormX.FormPaint;
Begin
OnPaint := Nil; // make sure handler is only called once
Application.ProcessMessages; //make sure all controls are painted
...do stuff
Modalresult := mrOK;
End;

The OnPaint handler is called when the modal message loop is already
running.

0 new messages