Why would something fail in FormCreate, but work fine in FormShow?
I'm using Delphi 7. When I try to create an object in FormCreate, it fails (actually it just hangs). When I try to do the same thing in FormShow, it works. Please note, I'm not talking about creating a visual component like a TEdit. It's a database abstraction object. Any ideas why this might happe开发者_开发知识库n? Are there any guidelines about this topic?
1) The reason is because on FormCreate, the handle to Form is not created yet.
If your database object needs a form handle, do this:
Self.HandleNeeded; // on FormCreate time.
2) The other reason is maybe your database component needs to be connected and it's only connected on DFM?
My first guess is that you're accessing a DataModule hasn't been created yet. If your project's source looks like the following:
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.CreateForm(TDataModule1, DataModule1);
Application.Run;
end.
And your TForm1.FormCreate
looks like the following:
begin
DataModule1.AddUsersToStringList(Self.ComboBox1.Items);
end;
Then FormCreate
going to fail because it's is being run as part of the Application.CreateForm(TForm1, Form1);
line, and your Data Module hasn't been created yet.
There's 2 solutions:
- Defer your processing/initialization until after all the forms and data modules have been created.
- Create all the data modules before creating any of your forms. The Application's "Main Form" in Delphi is the first
TCustomForm
descendant created byApplication.CreateForm
, not the first object.
精彩评论