Recent

Author Topic: Application with 2 forms  (Read 7929 times)

No_name

  • Guest
Application with 2 forms
« on: July 19, 2004, 06:24:55 pm »
Hello, I'm new at Lazarus and I've a problem: in my application there are 2 forms: the main and another. How can I show  the second after menu click ?
I've tried with:
Form2.Create;
Form2.Show;
but always said me "Access violation"

Best regard,
   by E0S

Giuseppe Ridinò

  • Full Member
  • ***
  • Posts: 130
Application with 2 forms
« Reply #1 on: July 21, 2004, 03:28:18 pm »
My application has 3 forms and I have no problems. 8)
 :arrow: Just use Form2.Show.
Form2.Create isn't necessary if you create the Form2 with Lazarus IDE.
You need to use Form2.Create only if you build it in run-time.

Vincent Snijders

  • Administrator
  • Hero Member
  • *
  • Posts: 2661
    • My Lazarus wiki user page
Application with 2 forms
« Reply #2 on: July 21, 2004, 03:40:09 pm »
Forms are normaly autocreated and a reference is stored in a global variable (Form2 in your case), if you don't indicate otherwise as pepecito mentioned.

Also remember a form is an object.

So to create a form at run-time, you have to do something like:
Code: [Select]

var
  MyForm2: TForm2;
begin
  MyForm2 := TForm2.Create;
  MyForm2.ShowModal;
  MyForm2.Free; //if you use Show, be sure to free it in an OnClose event.
end;

trlpht

  • Guest
2 forms - create
« Reply #3 on: July 26, 2004, 05:10:04 am »
--- snipped ---
So to create a form at run-time, you have to do something like:
Code: [Select]

var
  MyForm2: TForm2;
begin
  MyForm2 := TForm2.Create;
  MyForm2.ShowModal;
  MyForm2.Free; //if you use Show, be sure to free it in an OnClose event.
end;


The access violation occurs when you are not sure of who owns the form.  Do you not need to pass a parameter to the create method to specify this?  I was getting this error and it left me when I created a form in this manner:
Code: [Select]

...
begin
MyForm2.Create(Application);
MyForm2.Show
end;
...

Regards

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2583
Re: 2 forms - create
« Reply #4 on: July 29, 2004, 11:15:24 am »
Code: [Select]

...
begin
MyForm2.Create(Application);
MyForm2.Show
end;
...


You cant use the variable MyForm2 to create a class. The variable has first to be assigned. So use  
Code: [Select]
MyForm2 := TMyForm2.Create(Application);

Contrary to C++ the MyForm2 variable is just a pointer to an object (which has to be created first). Declaring a objectr variable doesn't mean that it is created.
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

 

TinyPortal © 2005-2018