Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FormCreate in Delphi problem

Tags:

forms

delphi

I'm working with someone else's code, and I'm adding a new form

So, I've created the form and I can open it, use the buttons and list, etc, but I'm having a problem doing things on formcreate.

I make the form by doing this:

procedure TModelForm.RepeatOpen(Sender: TObject);
var
 DefForm : TForm5;
begin
 DefForm := TForm5.Create(Self);

 Self.Visible := False;
 try
  DefForm.ShowModal;
 finally
  Self.Visible := True;
  DefForm.Release;
 end;
end;

in my TForm5, I have a procedure

procedure TForm5.FormCreate(Sender: TObject);
begin
 inherited;
 RunList := CModelList.Create;
 RunList.ReadData;
 RunList.FillList(ListBox1.Items);
end;

but it doesn't do anything

I also have

procedure TForm5.PopulateListClick(Sender: TObject);
begin
 RunList := CModelList.Create;
 RunList.ReadData;
 RunList.FillList(ListBox1.Items);
end;

which is assigned to a button, and this actually works and populates my ListBox

I've been looking it up online and it seems like there is no OnCreate function, there is a way to override it but it seems like there should be a way to just define what happens when the frame is first created

also, the reason I'm using FormCreate is because that's what the code I'm working with is doing, and it seems to be working

Thanks!

like image 592
KingKong Avatar asked Dec 22 '22 14:12

KingKong


2 Answers

You've probably forgotten to assign FormCreate to OnCreate. Personally I'd do it by overriding the constructor and so keeping the .dfm form out of the way.

As an aside I would like to comment on the code you wrote:

DefForm := TForm5.Create(Self);
Self.Visible := False;
try
  DefForm.ShowModal;
finally
  Self.Visible := True;
  DefForm.Release;
end;

You don't need to assign an owner to DefForm since you are taking on the task of cleaning up, although it generally does no harm to assign an owner. What's more the try/finally is try to do two jobs but it can only really do one. The call to Release is not needed, you can just call Free.

I'd write it like this:

DefForm := TForm5.Create(nil);
try
  Self.Visible := False;
  try
    DefForm.ShowModal;
  finally
    Self.Visible := True;
  end;
finally
  DefForm.Free;
end;
like image 129
David Heffernan Avatar answered Jan 02 '23 16:01

David Heffernan


Do you mean, that your eventhandler is not executed? If so, did you maybe just forgot to assign the procedure to the Form's OnCreate property?

like image 27
Robert J. Avatar answered Jan 02 '23 17:01

Robert J.