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

发布于 2024-12-21 16:10:09 字数 379 浏览 1 评论 0原文

使用德尔福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;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半衾梦 2024-12-28 16:10:09

那么,要创建一个 TEdit,您需要执行以下操作:

创建一个要使用的变量。局部变量或类成员。

Edit: TEdit;

然后你构建它。

Edit := TEdit.Create(Self);

构造函数的参数是所有者。这确保了当其所有者被销毁时该控件也被销毁。我的假设是 Self 是一种形式。

现在您需要为该控件提供一个父控件。

Edit.Parent := Self;

或者也许它在面板上。

Edit.Parent := StatusPanel;

最后,设置文本。

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

对于标签来说,一切都非常相似,只是您使用 Caption 属性而不是 Text 属性。

您肯定会想要设置其他属性,但我想您已经知道如何做到这一点。

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.

你不是我要的菜∠ 2024-12-28 16:10:09

您还可以直观地设计组件,对它们使用GExperts Components to Code Expert,然后再次从表单设计器中删除它们。对于标签/编辑对,这给出了类似

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;

大多数时候它需要一些返工(删除 TabOrder 行,用 SetBounds 替换 Left/Top/... 的东西,对齐或您自己的逻辑,...)以及对于某些属性/组件它根本不起作用。但这样你可以节省很多时间。

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. 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. But you can save a lot of time that way.

恏ㄋ傷疤忘ㄋ疼 2024-12-28 16:10:09
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;

令人困惑的是设置父级。

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.

无人问我粥可暖 2024-12-28 16:10:09
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;
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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文