简体   繁体   English

如何在delphi中动态创建组件,如TLabel或TEdit等

[英]how to dynamically create a component in delphi such as TLabel or TEdit …etc

Using Delphi 2010 使用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;

Well, to create a TEdit you need to do the following: 好吧,要创建一个TEdit您需要执行以下操作:

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. 我的假设是Self是一种形式。

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. 除了使用Caption属性而不是Text属性之外,使用标签它们都非常相似。

And you will surely want to set other properties but I guess you already know how to do that. 你一定会想要设置其他属性,但我想你已经知道如何做到这一点。

Var
  AnEdit : TEdit;
Begin
  AnEdit := TEdit.Create(self);
  AnEdit.Parent := self; // or some suitable container compoent e.g GroupBox, Panel
  AnEdit.Top := ?;
  AnEdit.Left := ?
  // any other properties you weant to set.
End;

The bit that catches people out is setting parent. 吸引人们的是设置父母。

You can also design the components visually, use the GExperts Components to Code expert on them and then delete them from the form designer again. 您还可以直观地设计组件,使用GExperts组件对其进行代码编码 ,然后再次从表单设计器中删除它们。 For a label/edit pair this gives something like 对于标签/编辑对,这给出了类似的东西

var
  Edit1: TEdit;
  Label1: TLabel;

  Edit1 := TEdit.Create(Self);
  Label1 := TLabel.Create(Self);

  Edit1.Name := 'Edit1';
  Edit1.Parent := Self;
  Edit1.Left := 344;
  Edit1.Top := 172;
  Edit1.Width := 121;
  Edit1.Height := 21;
  Edit1.TabOrder := 0;
  Edit1.Text := 'Edit1';
  Label1.Name := 'Label1';
  Label1.Parent := Self;
  Label1.Left := 296;
  Label1.Top := 176;
  Label1.Width := 65;
  Label1.Height := 17;
  Label1.Caption := 'Label1';
  Label1.FocusControl := Edit1;

Most of the times it needs some reworking (remove the TabOrder lines, replace the Left/Top/... stuff by SetBounds, Align or your own logic, ...) and for some properties/components it doesn't work at all. 大多数时候它需要一些重做(删除TabOrder行,用SetBounds替换Left / Top / ...东西,Align或你自己的逻辑,......)对于某些属性/组件它根本不起作用。 But you can save a lot of time that way. 但是你可以节省很多时间。

with TEdit.Create(self) do
begin
  Parent:= ... // The name of the panel or form, on which you would like to place TEdit
  Text:= 'your text'; 
  // And you could set its position by giving "Left" and/or "Width", so on..
end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM