How to create a form programmatically with a couple components on it in Delphi

后端 未结 2 791
抹茶落季
抹茶落季 2020-12-28 09:12

I\'m working with Delphi 7 and I\'m trying to create a form programmatically. Here\'s my form class stub:

unit clsTStudentInfoForm;

interface

    uses Form         


        
2条回答
  •  感动是毒
    2020-12-28 10:06

    Creating modal forms with controls on the fly is easy:

    procedure CreateGreetingForm;
    var
      frm: TForm;
      lbl: TLabel;
      edt: TEdit;
      btn: TButton;
    begin
    
      frm := TForm.Create(nil);
      try
        lbl := TLabel.Create(frm);
        edt := TEdit.Create(frm);
        btn := TButton.Create(frm);
        frm.BorderStyle := bsDialog;
        frm.Caption := 'Welcome';
        frm.Width := 300;
        frm.Position := poScreenCenter;
    
        lbl.Parent := frm;
        lbl.Top := 8;
        lbl.Left := 8;
        lbl.Caption := 'Please enter your name:';
    
        edt.Parent := frm;
        edt.Top := lbl.Top + lbl.Height + 8;
        edt.Left := 8;
        edt.Width := 200;
    
        btn.Parent := frm;
        btn.Caption := 'OK';
        btn.Default := true;
        btn.ModalResult := mrOk;
        btn.Top := edt.Top + edt.Height + 8;
        btn.Left := edt.Left + edt.Width - btn.Width;
    
        frm.ClientHeight := btn.Top + btn.Height + 8;
        frm.ClientWidth := edt.Left + edt.Width + 8;
    
        if frm.ShowModal = mrOk then
          ShowMessageFmt('Welcome, %s', [edt.Text]);
    
      finally
        frm.Free;
      end;
    
    end;
    

提交回复
热议问题