在我的Delphi程序中,我有一个登录表单,它在创建主表单之前显示,但我面临的问题是我想登录检查在主表单中处理,这意味着登录表单将使用要检查并继续的主要表格, 请阅读以下评论:
请阅读以下评论:
procedure LogInButtonClick(Sender: TObject) ;
这是TLoginForm代码(from delphi.about.com):
unit login; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TLoginForm = class(TForm) LogInButton: TButton; pwdLabel: TLabel; passwordEdit: TEdit; procedure LogInButtonClick(Sender: TObject) ; public class function Execute : boolean; end; implementation {$R *.dfm} class function TLoginForm.Execute: boolean; begin with TLoginForm.Create(nil) do try Result := ShowModal = mrOk; finally Free; end; end; procedure TLoginForm.LogInButtonClick(Sender: TObject) ; begin if passwordEdit.Text = 'delphi' then { Here how it's possible to use : if MainForm.text=passwordEdit.Text then ModalResult := mrOK } ModalResult := mrOK else ModalResult := mrAbort; end; end.
这是主程序初始化流程:
program PasswordApp; uses Forms, main in 'main.pas' {MainForm}, login in 'login.pas' {LoginForm}; {$R *.res} begin if TLoginForm.Execute then begin Application.Initialize; Application.CreateForm(TMainForm, MainForm) ; Application.Run; end else begin Application.MessageBox('You are not authorized to use the application. The password is "delphi".', 'Password Protected Delphi application') ; end; end.
谢谢
如果您需要首先创建主表单,请先创建它:begin Application.Initialize; Application.CreateForm(TMainForm, MainForm);//created, but not shown if TLoginForm.Execute then//now the login form can refer to the main form Application.Run//this shows the main form else Application.MessageBox('....'); end;
这是你提出的问题的直接和天真的答案.更广泛地思考,我鼓励您将登录测试移出主表单.把它放在任何更高级代码需要使用的地方.您目前正在努力的设计具有不健康的耦合.