如何在delphi中动态创建组件,例如TLabel或TEdit ... etc [英] how to dynamically create a component in delphi such as TLabel or TEdit ...etc

查看:245
本文介绍了如何在delphi中动态创建组件,例如TLabel或TEdit ... etc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Delphi 2010

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;

推荐答案

好,要创建TEdit,您需要执行以下操作:

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;

然后构建它.

Edit := TEdit.Create(Self);

构造函数的参数是所有者.这样可以确保在销毁控件的所有者时销毁该控件.我的假设是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;

或者可能是在面板上.

Edit.Parent := StatusPanel;

最后,设置文本.

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

带有标签的所有内容都非常相似,不同之处在于,您使用的是Caption属性而不是Text属性.

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.

这篇关于如何在delphi中动态创建组件,例如TLabel或TEdit ... etc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆