Is it possible to dynamically create form without having *.dfm and *.pas files?

穿精又带淫゛_ 提交于 2019-12-10 13:39:18

问题


is it possible to create and show TForm without having source files for it ? I want to create my forms at runtime and having the empty *.dfm and *.pas files seems to me useless.

Thank you


回答1:


Do you mean like this?

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;



回答2:


Yes, it is possible:

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

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


来源:https://stackoverflow.com/questions/8595380/is-it-possible-to-dynamically-create-form-without-having-dfm-and-pas-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!