how to dynamically create a component in delphi such as TLabel or TEdit ...etc

Rafik Bari picture Rafik Bari · Dec 16, 2011 · Viewed 14.6k times · Source

Using Delphi 2010

SQLQuery1.First; // move to the first record
while(not SQLQuery1.EOF)do begin
   // do something with the current record
   // What's the code should i write in this part in order to create a TEdit
   // containing the user fullname the current item.
   ShowMessage(SQLQuery1['whom']);
   SQLQuery1.Next; // move to the next record
end;

Answer

David Heffernan picture David Heffernan · Dec 16, 2011

Well, to create a TEdit you need to do the following:

Create a variable to work with. Either a local variable or a class member.

Edit: TEdit;

Then you construct it.

Edit := TEdit.Create(Self);

The parameter to the constructor is the owner. This ensures that the control is destroyed when its owner is destroyed. My assumption is that Self is a form.

Now you need to give the control a parent.

Edit.Parent := Self;

Or perhaps it's on a panel.

Edit.Parent := StatusPanel;

Finally, you set the text.

Edit.Text := SQLQuery1['whom']);

With a label it's all very similar except that you use the Caption property rather than the Text property.

And you will surely want to set other properties but I guess you already know how to do that.