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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I have a problem recreating a form.

Status
Not open for further replies.

BobbaFet

Programmer
Feb 25, 2001
903
NL
This is my problem in detail:

I have two forms that the user can see. A third is the application's main form. It just has variables stored used by both forms. Now my problem is this:

They close one form and then subsequently open the other
one. This goes well. All the timers and loops are killed before the form closes so there is no wasted memory. They get their second form, but when they want to go back, it
application freezes.

This is the code i use for closing the first form:

Self.Close;
Application.CreateForm(TForm2,Form2);

(with in the OnClose event Action := caFree;)

And the code to go back is the same.

I think this should work. Simple enough right ??? Well,
apperantly not, because this obviously doesnt work.

I hope you guys have some hints tips or info for the
solution. I do not want to have to split up my application
into two diff parts.

Hope you can help me out, [bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
 
hi,

Looking at your code the creation of the second form is not happening. The from is closed and the line for creating is not executed. I think you create the forms the first time with the start of the application and the second time programatically. So i think to change the order of the lines should do the trick. First create form2 and then close form1.

Steph [Bigglasses]
 
We sometimes have issues recreating forms when the form is freed properly. We do this:

if form2 = nil then
form2 := Tform2.Create(nil);
form2.show;

We also set the form to nil in the formclose:
self.release;
self := nil;
 
I cant have both form open at the same time because of the HUGE numbers of components on them. One has a 180 and the
other has 233.

SvanHooft, the second form is created, and everything does
work fine. But I cant go back to the first form after closing it.

But if what you say is true, could it be an good idea to try
out letting the mainform do the opening and closing of
forms ???

Im gonna try that out, that way it at least doesnt in which
order the code is right ??? Since the mainform will always
be there, it only contains a timer, which is freed after its
used.

Thanks for your responses, guys.
Ill try to get it to work with your tips, [bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
 
Hi again,

I tried using the mainform to close and create the
appropriate forms. But that doesnt work either. What I did
was this:

I put their TForm names and reference form names in two
arrays, called MyTFormsArray and MyRFormsArray as global vars.

MyRFormsArray: array[1..2] of TForm;
MyTFormsArray: array[1..2] of TFormClass;

And filled them with the appropriate names.
Then i did this:

I made a new procedure (so that i can access it from other
forms) like this:

procedure TMCU.HideAndCreateForm(FreeForm: Integer; LoadForm: Integer);
begin
MyRFormsArray[FreeForm].Hide;
MyRFormsArray[FreeForm].Close;

Application.CreateForm(MyTFormsArray[LoadForm], MyRFormsArray[LoadForm]);
MyRFormsArray[LoadForm].ShowModal;
MyRFormsArray[LoadForm].Show;
end;

So then I put under the switch button (to switch the forms)
this code:

(MCU stands for Main Control Unit)

MCU.HideAndCreateForm(1,2);

or

MCU.HideAndCreateForm(2,1);

And I did put the self.release; and the self := nil; in the
close events of both the forms. But this doesnt work either.
Im really confused now. It doesnt have anything to do with
loops still running because they are disabled as soon as the
canclose is set to true, they all break. Timers are
disabled in the OnCloseQuery, so that doesnt seem to be the
problem.

The error I keep getting is EAccessViolation and an EInvalidPointer operation.

I dont know whats wrong with it though. [bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
 
I made it like that because Im planning to add more forms
and this way i can extend it by expanding the array.

Ow yeah, did I already mention that this is driving me nuts?

Please help save a programmer's sanity !!! [bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
 
Do you "ReFill" your array of TForms? Because if you don't, i don't really understand how it supposed to work. When you Free a from a set pointer to it to nil and then create a new instance of that form, you get a new pointer to it, and if you try to use a reference to your form stored in your array, you'll get an AV. I know nothing about your implementation, but from the information you've supplied it appears that you don't reinitialize you array.

--- markus

P.S. Wouldn't it be more convinient to store poiter to your forms in TList?
 
First, I would say that TList is appropriate for holding objects, not an array, but that's my own opinion. The other thing I like to do is use class funtions (static). Here's an example.

type
TMyForm = class(TForm)
...
class Function Execute(AOwner : TComponent) : tModalResult;
end;

var
MyForm : TMyForm;

implementation

class Function TMyForm.Execute(AOwner : TComponent) : TModalResult;
begin
MyForm := TMyForm.Create(AOwner);
try
Result := MyForm.ShowModal;
finally
FreeAndNil(MyForm);
end;
end;

//When you want to show the form, call it as
TMyForm.Execute(Application);

The beauty is simplicity itself, the form is freed, no memory leaks and one line to create the form. If you want to repeatedly open the form just call TMyForm.Execute(self) over again without worries.

 
From looking at your code, I agree, you can not close form number one before you create form number two. When form number 1 is closed, you have nothing anywhere. You should do something like this:


//*** THIS WILL CREATE YOUR FORM 2.
Application.CreateForm(TForm2,Form2);

//*** THIS WILL OPEN YOUR SECOND FORM.
Form2.Show;

//*** THIS WILL CLOSE FORM ONE.
Close;

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top