Form creation using names in a table

I have the names of the forms my application uses written in a field of a table. What I need to do is find the appropiate record (based on user actions) and showmodal the corresponding form using the name written in the field. How can I do this?

Thanks A LOT in advance for your help !!
ajgonzalezmAsked:
Who is Participating?
 
rwilson032697Commented:
Here's a working code sample :-)

type
  TFormClass = class of TForm;

procedure TForm1.Button1Click(Sender: TObject);
var
  TheClass : TPersistentClass;
  TheForm : TForm;
begin
  TheClass := GetClass('TFORM1');
  TheForm := TFormClass(TheClass).Create(Application);
  TheForm.ShowModal;
  TheForm.Release;
end;

Note that you need to register the classes you get GetClass to find, like this:

initialization
  registerclass(TForm1);

or

initialization
  registerclasses([TForm1, TForm2, ...]);


Cheers,

Raymond.

0
 
rwilson032697Commented:
This should do it:

var
  TheFormClass : TPersistentClass;
  TheForm : TForm;

  TheFormClass := GetClass(TheTable.FieldByName('FormName').AsString);
  Application.CreateForm(TheFormClass, TheForm);
  TheForm.ShowModal;
  TheForm.Release;

Cheers,

Raymond.
0
 
ajgonzalezmAuthor Commented:
Hi, Raymond:

I didn't accept your answer just because I tried your suggestion on the computer and the compiler said: Incompatible types: ‘TComponentClass’ and ‘TPersistentClass’ on the line saying Application.CreateForm...

Could you please help me further?

THANK YOU !!!

AJ :-)
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
rwilson032697Commented:
You could use this line to create the form instead:

 TheForm := (TheFormClass as TForm).Create(Application);

Cheers,

Raymond.
0
 
ajgonzalezmAuthor Commented:
Sorry... now I got an 'Operator not applicable to this operand type' on the assigment line you suggested...

Are you willing to help me further?

Thank you :-)

AJ
0
 
ajgonzalezmAuthor Commented:
THANKS A LOT RWilson !!! That was excellent !!!

I hope to become an expert like you in the near future...
by now I'm just a beginner.

Thanks again !!!

AJ
0
 
rwilson032697Commented:
Glad I could help - sorry about the false starts :-)

Cheers,

Raymond.
0
 
lfgmartinsCommented:
Hi,
How can I do a  registerclass(TForm1), with a the name passed from a string var ('TForm1'). I need this to alllow dynamic form names. What else I'd have to do a registerclass([TUser, TContracts,...]) form my 50 forms.
0
 
rwilson032697Commented:
Use the GetClass function to return the class name into a variable of type TClass and pass that to RegisterClass() as above.

Cheers,

Raymond.
0
 
lfgmartinsCommented:
unfortanelly don't work because the getclass() returns nil if the class is not registered. So I cannot apply RegisterClass next.
Thanks.
0
 
rwilson032697Commented:
Where are the forms coming from then?
0
 
lfgmartinsCommented:
I've found a way I first need to registerclass in initialization of that form. Then when I getclass it don't give error :)
Thanks,
0
 
rwilson032697Commented:
Great!

Cheers,

Raymond.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.