![](/img/trans.png)
[英]How to get all components (Tlabel,Tedit …) in a form in Delphi?
[英]how to dynamically create a component in delphi such as TLabel or TEdit …etc
使用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
您需要執行以下操作:
創建一個可以使用的變量。 局部變量或類成員。
Edit: TEdit;
然后你構建它。
Edit := TEdit.Create(Self);
構造函數的參數是所有者。 這可確保在銷毀其所有者時銷毀該控件。 我的假設是Self
是一種形式。
現在你需要給控件一個父級。
Edit.Parent := Self;
或者也許是在面板上。
Edit.Parent := StatusPanel;
最后,設置文本。
Edit.Text := SQLQuery1['whom']);
除了使用Caption
屬性而不是Text
屬性之外,使用標簽它們都非常相似。
你一定會想要設置其他屬性,但我想你已經知道如何做到這一點。
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;
吸引人們的是設置父母。
您還可以直觀地設計組件,使用GExperts組件對其進行代碼編碼 ,然后再次從表單設計器中刪除它們。 對於標簽/編輯對,這給出了類似的東西
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 / ...東西,Align或你自己的邏輯,......)對於某些屬性/組件它根本不起作用。 但是你可以節省很多時間。
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.