delphi dynamically created controls

 

 Dynamically Create Controls

 

In fact, dynamically creating controls very simple, I believe that after reading this article you will understand the whole.

 

1 in which register initialization portion of the cell, (so that when using the unit automatically registers) As:

 

RegisterClass( TButton );

 

2 or using FindClass GetClass function to obtain a reference class, such as:

 

var MetaClass : TPersistentClass; 

MetaClass := FindClass( 'TForm2');

 

3 maps MetaClass general type, or you can not call the correct constructor (because you did not specify the required parameters)

 

var ComponentMetaClass : TComponentClass;

ComponentMetaClass := TComponentClass( MetaClass );

 

 

4 Call constructor 

 

var AForm: TComponent;

AForm := ComponentMetaClass.Create( self );

 

5 AForm now a valid instance of the class, it may invoke the method.

 

TForm(AForm).Show;

 

or

 

was Some Form: TForm;

SomeForm := TForm( AForm );

SomeForm.Show;

 

Complete example:

 

procedure TForm1.Button1Click(Sender: TObject); 

var MetaClass : TPersistentClass;

ComponentMetaClass : TComponentClass;

AForm: TComponent;

begin

  MetaClass := FindClass( 'TForm2');

  ComponentMetaClass := TComponentClass( MetaClass );

  AForm := ComponentMetaClass.Create( self ); 

  TForm(AForm).Show;

end;

 

or it could be

TFormTForm( TComponentClass( FindClass( 'TForm2' ) ).Create( Self )).Show;

 

 

Guess you like

Origin www.cnblogs.com/blogpro/p/11453415.html