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!

Newbie Question:- Error destorying form dynamically created 1

Status
Not open for further replies.

Jasonic

Technical User
Jun 20, 2001
69
GB
I dynamically create a connection form with several components on it

3 x TLabel's
2 x TComobBox's
1 x TEdit
2 x TButton's

1 Button processes the connection
the other button cancels the connection and closes the form

with only two buttons on the form everything works fine as soon as I add any other components an exception error occurs.

I create each component as Create(Tform) NB. Tform is the dynamic form.

I also set the parent to TForm.

My Cancel button has the following code :-

GetParentForm(Sender as TControl).Destroy;

Any Ideas
or more information required

thanks in advance
Jason Jason Thomas
AssetCenter Consultant
:)
 
The way you free your form is not correct. You are calling free from within the form. This is not possible. A form can only be freed by another form or object or its parent.

So, There is a function where you create the form dynamicly.
Let's say it's form1. From within form1 you create the dynamic form fom2. Your function should look like this:

procedure CreateADynamicForm(Sender: TObject)
var
form2: TForm2;
begin
form2 := TForm2.Create(Application);
try
form2.ShowModal;
finally
form2.Free;
end;
end;

The form form2 is created in form1 and freed by form1.
This is the correct way of creating and freeing a form.
 
Thanks for the reply, but if I have this form with a cancel button on it, how do I close the form once I have clicked the cancel button? Jason Thomas
AssetCenter Consultant
:)
 
Clive

Thanks for the obvious, but I had tried that with just close, and it closes the whole application.

I have just tried it with
GetParentForm(Sender as TControl).close;
and seems to work a treat

Thanks for the pointer Jason Thomas
AssetCenter Consultant
:)
 
If you called the form with:

form2.showmodal

then you can set the cancel's buttons modalresult property.
If you set it to mrCancel than the form will be closed automaticly.

If you called it with:

form2.show

then you have to close the form by calling form2.close.

Remind that closing a form doesn't mean that it is freed. After you closed it you have to free it with form2.free if it has been created dynamicly !
 
I just checked what I have used in the past to close forms (that aren't the main form). Here is what I have successfully used in the past:
Code:
ModalResult := mrCancel;
Clive [infinity]
Ex nihilo, nihil fit (Out of nothing, nothing comes)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top