Discussion:
create controls at run time
(too old to reply)
didan
2008-02-04 18:40:39 UTC
Permalink
I would like to create a TForm at run-time and then add some controls on this form. Could you please give me a example code?
Thanks for help
Clayton Arends
2008-02-04 20:27:11 UTC
Permalink
Post by didan
I would like to create a TForm at run-time and then add some controls on
this form. Could you please give me a example code?
If you want to create a form at run-time that you have designed then you can
create it a couple of ways:

// This creates a form with 'Application' as its owner. This means that
// the form will get destroyed when the application object gets destroyed
// (which is when the application closes). You can use other TComponent
// descendants here instead of 'Application' (the main form for example).
TMyForm* form = new TMyForm(Application);

// This creates a form with no owner. You are responsible for freeing
// the form when you are finished using it.
TMyForm* form = new TMyForm(NULL);

// You can use the VCL way of creating the form (which is basically the
// same as the first option I showed above)
TMyForm* form;
Application->CreateForm(__classid(TMyForm), &form);

If you want to create a generic blank form that is not based on any visually
designed form then use this code instead:

// This will create a form and instruct the VCL to skip the resource
// loading step
TForm* form = new TForm(Application, 0);

// ... or with no owner
TForm* form = new TForm(NULL, 0);

Once you have your form you can create any control or component you'd like
(assuming you've included the correct header files). For example, if you'd
like to create an edit control:

TEdit* edit = new TEdit(form);
edit->Parent = edit;
edit->Left = 40;
edit->Top = 20;

All other controls behave the same way.

Post again if you need more pointers or if a concept is not making sense.

HTH,
Clayton
didan
2008-02-06 13:19:26 UTC
Permalink
Hi Clayton
Thanks!!! great help

Continue reading on narkive:
Loading...