2011-12-21 70 views

回答

10

你的意思是這樣的嗎?

procedure TForm1.Button1Click(Sender: TObject); 
var 
    Form: TForm; 
    Lbl: TLabel; 
    Btn: TButton; 
begin 

    Form := TForm.Create(nil); 
    try 
    Form.BorderStyle := bsDialog; 
    Form.Caption := 'My Dynamic Form!'; 
    Form.Position := poScreenCenter; 
    Form.ClientWidth := 400; 
    Form.ClientHeight := 200; 
    Lbl := TLabel.Create(Form); 
    Lbl.Parent := Form; 
    Lbl.Caption := 'Hello World!'; 
    Lbl.Top := 10; 
    Lbl.Left := 10; 
    Lbl.Font.Size := 24; 
    Btn := TButton.Create(Form); 
    Btn.Parent := Form; 
    Btn.Caption := 'Close'; 
    Btn.ModalResult := mrClose; 
    Btn.Left := Form.ClientWidth - Btn.Width - 16; 
    Btn.Top := Form.ClientHeight - Btn.Height - 16; 
    Form.ShowModal; 
    finally 
    Form.Free; 
    end; 

end; 
+1

啊,我認爲對於動態創建表單我需要這些文件,我不會相信這很容易(下次我會試着問)。謝謝 – 2011-12-21 19:55:46

+4

@Martin .dfm文件解析將.dfm文件轉換爲屬性分配,就像Andreas出色的答案中的代碼一樣。 – 2011-12-21 19:57:53

+2

+1好答案。在附註中,您不必爲添加到表單的每個控件使用變量。例如,您可以使用'with TLabel.Create(Form)do'來添加標籤並修改其屬性。 Delphi會爲它分配一個唯一的名稱,如果你願意,你可以改變它。 – 2011-12-21 20:23:50

3

是的,這是可能的:

procedure TForm1.Button1Click(Sender: TObject); 
var 
    Form: TForm; 

begin 
    Form:= TForm.Create(Self); 
    try 
    Form.ShowModal; 
    finally 
    Form.Free; 
    end; 
end; 
相關問題