这是一个有趣的案例。德尔福 - 表单属性FormCreate上设置时从其他表单
有登录表单,其中设置了一些变量值,如company_id和SelectedlanguageCode,想法是将此值传递给MainForm属性以使事情更快(所选语言代码转到数据库进行查询),则此值将在FormCreate事件中立即需要,但是这里的值缺失,但在CreateForm事件后调试值刚刚显示。
这里是项目源代码:
{$R *.res}
Var
LoginOK: Boolean = False;
sCompanyId: integer;
sSelectedLanguageCode: string;
begin
// The login form is created and show up
// --------------------------------------
fLogin := TfLogin.Create(nil);
try
// Show login form. When it closes, see if login worked.
fLogin.ShowModal;
LoginOK := fLogin.CanLogin;
sCompanyId := fLogin.pCompanyId;
sSelectedLanguageCode := fLogin.gDefaultLanguageCode;
finally
fLogin.DisposeOf;
end;
if not LoginOK then
Halt; // Login failed - terminate application.
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TfMain, fMain);
// Here the properties of Main are set with the values,
// when debugging the properties get the values, you can
// see the actual values when hovering over the property
fMain.gSelectedLanguage := sSelectedLanguageCode;
fMain.gUserCompany := sCompanyId;
Application.Run;
end.
但是如前所述,在主要的的CreateForm,属性是空的...... 还有......事件的CreateForm后,性能有实际的价值。
我需要在活动的CreateForm这个值,因为一些方法将被调用,并需要向他们发送...
我欣赏的是怎么回事就明白我在做什么错了,或者如果有答案这是一个很好的理由,如果你能给我另一种方法来解决这个要求,谢谢。
如果我正确地理解你,你有鸡和鸡蛋的困境。您无法在创建表单之前设置属性,但在创建表单时您已经需要这些值。因此,将这些变量放在主窗体单元中作为全局变量(颤抖)。然后,您可以在创建表单之前设置它们,并且您的主窗体可以在需要时读取它们。 –
当Application.CreateForm返回时,表单已经被创建并且它的FormCreate被执行。很显然,当你没有设置它们时,它们不能在'FormCreate'中使用,直到*'FormCreate'已经运行之后。 –
Tom,就是这样做的,提供的代码是应用程序代码,它首先创建登录表单,然后在用户登录后创建主表单并分配登录的值,发布登录表单和显示Main。我一直在主要属性中使用它们的值,这是完美的,但直到我在CreateForm事件中需要它们时,才意识到具体值的存在尚未提供,只是在那里。 – VisualFox