Sde*_*ean 4 forms delphi login
在我的Delphi程序中,我有一个登录表单,它在创建主表单之前显示,但我面临的问题是我想登录检查在主表单中处理,这意味着登录表单将使用要检查并继续的主要表格,
请阅读以下评论:
过程LogInButtonClick(Sender:TObject);
这是TLoginForm代码(来自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.
Run Code Online (Sandbox Code Playgroud)
这是主程序初始化流程:
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.
Run Code Online (Sandbox Code Playgroud)
谢谢
Dav*_*nan 11
如果您需要首先创建主表单,请先创建它:
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;
Run Code Online (Sandbox Code Playgroud)
这是你提出的问题的直接和天真的答案.更广泛地思考,我鼓励您将登录测试移出主表单.把它放在任何更高级代码需要使用的地方.您目前正在努力的设计具有不健康的耦合.
我通常从OnCreate开始执行此操作MainForm;或者从OnCreate的DataModule,如果你有的话。例如:
TMainForm.OnCreate(Sender: TObject);
var F: TLoginForm;
begin
F := TLoginForm.Create(Self);
try
F.ShowModal;
finally F.Free;
end;
end;
Run Code Online (Sandbox Code Playgroud)
我不喜欢DPR过多地弄乱文件。这是有效的,以正确的顺序显示表单,如果是TMainForm由 Delphi 自动创建的,那么该MainForm变量已经被分配并可以在触发时使用OnCreate;
PS:访问MainForm变量实际上是糟糕的设计,但如果你想要的话它就在那里。
| 归档时间: |
|
| 查看次数: |
9926 次 |
| 最近记录: |