delphi登录表单 [英] delphi Login Form

查看:244
本文介绍了delphi登录表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Delphi程序中,我有一个登录表单,它在主表单创建之前显示,但是我面临的问题是我想要在主表单中处理登录检查,这意味着登录表单将使用主表单进行检查并继续,

请阅读以下内容的评论:


过程LogInButtonClick(发件人:TObject);

这里是TLoginForm代码( from delphi.about.com ):

 单元登录; 

接口

使用
Windows,消息,SysUtils,变体,类,
图形,控件,窗体,对话框,StdCtrls;

类型
TLoginForm = class(TForm)
LogInButton:TButton;
pwdLabel:TLabel;
passwordEdit:TEdit;
程序LogInButtonClick(发件人:TObject);
public
类函数执行:布尔型;
end;

实现
{$ R * .dfm}

类函数TLoginForm.Execute:boolean;
用TLoginForm.Create(nil)做

尝试
结果:= ShowModal = mrOk;
终于
自由;
end;
end;

procedure TLoginForm.LogInButtonClick(Sender:TObject);
begin
如果passwordEdit.Text ='delphi'then
{
在这里如何使用:
如果MainForm.text = passwordEdit.Text那么
ModalResult:= mrOK
}

ModalResult:= mrOK
else
ModalResult:= mrAbort;
end;

结束。

以下是主程序初始化流程:

 程序PasswordApp; 

在'main.pas'中使用
表单,
mainForm,
在'login.pas'中登录{LoginForm};

{$ R * .res}

begin
if TLoginForm.Execute then
begin
Application.Initialize;
Application.CreateForm(TMainForm,MainForm);
Application.Run;
end
else
begin
Application.MessageBox('您无权使用应用程序,密码为delphi。','密码保护的Delphi应用程序');
end;
结束。

谢谢

解决方案如果你需要首先创建主表单,那么首先创建它:

  begin 
Application.Initialize;
Application.CreateForm(TMainForm,MainForm); //已创建,但未显示
如果TLoginForm.Execute则//现在登录表单可以引用主表单
Application.Run //这显示了主要格式
else
Application.MessageBox('....');
end;






这是一个直接和天真的答案,你问。更广泛地思考,我鼓励你将登录测试移出主表单。把它放在可以被任何更高级代码需要使用的地方。您目前正在努力的设计有不健康的耦合。

in my Delphi program i've a Login Form and it's Displayed Before the Main Form is Created , but the issue that i'm facing is that i want to Login Check to be processed in the main form , that means the Login Form will use the Main Form to check and proceed ,

please read the comment placed in :

procedure LogInButtonClick(Sender: TObject) ;

here is the TLoginForm code ( 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. 

and here's the Main Program Initialization flow :

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.

thank you

解决方案

If you need the main form to be created first, then create it first:

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;


That's a direct and naive answer to the question that you asked. Thinking more broadly, I would encourage you to move the login testing out of the main form. Put it somewhere that can be used by whatever higher-level code needs to. The design you are currently working towards has unhealthy coupling.

这篇关于delphi登录表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆