Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi 6 create new form with constructor

Tags:

delphi

I am new to Delphi and have a problem with creating a new form dynamically. I want to create the new form with the elements properties from the gui I made. Here is the form I want to dynamically create :

unit AddEmployeeF;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons;

type
  TAddEmployee = class(TForm)
    GroupBox1: TGroupBox;
    AddName: TLabel;
    AddDept: TLabel;
    AddPhone: TLabel;
    AddExtension: TLabel;
    AddDetails: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    procedure CancelButtonClick(Sender: TObject);
 private
    { Private declarations }
  public
    constructor CreateNew(AOwner: TComponent; Dummy: Integer = 0); override;
  end;
var
  AddEmployee: TAddEmployee;

implementation

{$R *.dfm}
      constructor TAddEmployee.CreateNew(AOwner: TComponent; Dummy: Integer = 0; Detail : String);
begin
  inherited Create(AOwner);
  AddDetails.Caption := Detail;
end;


procedure TAddEmployee.CancelButtonClick(Sender: TObject);
begin
    self.Close;
end;

end.

I dont want to create all the gui elements again in the constructor, just to modificate some properties of the elements, like caption but keep the positions and other properties from the gui definition. It's possible? And how to create the form from another form, like this? :

procedure TWelcome.SpeedButton1Click(Sender: TObject);
var
myForm :TAddEmployee;
begin
        myForm := TAddEmployee.CreateNew(AOwner, Dummy, Details);

     myForm.ShowModal;

end;
like image 312
Kamy Andrei Avatar asked Nov 03 '13 07:11

Kamy Andrei


1 Answers

You overrode the wrong constructor. The TForm.CreateNew() constructor bypasses DFM streaming, so all of your design-time components will not be created at run-time. Worse, your overridden CreateNew() constructor is calling the inherited TForm.Create() constructor, which calls CreateNew() internally, thus you will get stuck in an endless loop that causes an stack overflow error at runtime.

To do what you are asking for, override the TForm.Create() constructor instead, or define a whole new constructor that calls TForm.Create() internally. Do not involve TForm.CreateNew() at all.

type
  TAddEmployee = class(TForm)
    ...
  public
    constructor Create(AOwner: TComponent); override; // optional
    constructor CreateWithDetail(AOwner: TComponent; Detail : String);
  end;

constructor TAddEmployee.Create(AOwner: TComponent);
begin
  CreateWithDetail(AOwner, 'Some Default Value Here');
end;

constructor TAddEmployee.CreateWithDetail(AOwner: TComponent; Detail : String);
begin
  inherited Create(AOwner);
  AddDetails.Caption := Detail;
end;

procedure TWelcome.SpeedButton1Click(Sender: TObject);
var
  myForm : TAddEmployee;
begin
  myForm := TAddEmployee.CreateWithDetail(AOwner, Details);
  myForm.ShowModal;
  myForm.Free;
end;
like image 70
Remy Lebeau Avatar answered Oct 22 '22 21:10

Remy Lebeau