2

我希望用户在他/她能够查看表单之前登录...

它似乎不起作用。

有任何想法吗?

sLoginID := InputBox('Ink Spots','Please enter login ID:','');
  sLoginPassword := InputBox('Ink Spots','Please enter password for ' + sLoginID,'');
  if sLoginID <> 'user'
    then
    begin
      ShowMessage('You shall not pass!');
      Self.Close;
    end
    else
    begin
      sLoginPassword := InputBox('Ink Spots','Please enter password for ' + sLoginID,'');
      if sLoginPassword <> 'pass'
        then
        begin
          ShowMessage('You shall not pass!');
          Self.Close;
        end;
    end

;

4

2 回答 2

10

如果表单不是要创建的,那么它应该从其构造函数中抛出一个异常。这是避免创建对象的定义方法。注意OnShowandOnCreate不是构造函数;您需要改写Create

就您而言,您试图在错误的地方解决问题。避免创建您并不真正想要的表单的更好方法是从一开始就不要创建它。与其创建表单然后检查是否允许,不如先检查是否允许,然后显示。

您可以将该操作包装到一个函数中,以方便调用者。例如:

class function TRijnhardtForm.ConditionallyCreate: TRijnhardtForm;
var
  LoginID, LoginPassword: string;
begin
  Result := nil;
  LoginID := InputBox(Application.Title, 'Please enter login ID:','');
  LoginPassword := InputBox(Application.Title, 'Please enter password for ' + LoginID, '');
  if LoginID <> 'user' then begin
    ShowMessage('You shall not pass!');
    Exit;
  end;
  LoginPassword := InputBox(Application.Title, 'Please enter password for ' + LoginID, '');
  if LoginPassword <> 'pass' then begin
    ShowMessage('You shall not pass!');
    Exit;
  end;
  Result := TRijnhardtForm.Create(Application);
end;

然后,您可以使用该方法创建表单,但前提是用户正确且密码输入两次。

RijnhardtForm := TRijnhardtForm.ConditionallyCreate;
于 2013-07-23T19:26:43.260 回答
0

在您的项目源...

program Project49;

uses
  Forms,
  Unit56 in 'Unit56.pas' {Form56};

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TForm56, Form56);
  Login;
//Most importantly...comment out Application.Run
//  Application.Run;
end.

然后将您的登录名更改为程序

unit Unit56;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm56 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }

  end;
  procedure Login;
var
  Form56: TForm56;

implementation

{$R *.dfm}
procedure Login;
var
 a_Login: boolean;
 sLoginId, sLoginPassword: string;
begin
 a_Login:= False;
 sLoginID := InputBox('Ink Spots','Please enter login ID:','');
//     sLoginPassword := InputBox('Ink Spots','Please enter password for ' + sLoginID,'');
 if sLoginID <> 'user'
 then
 begin
  ShowMessage('You shall not pass!');
  //Self.Close;
 end
 else
 begin
  sLoginPassword := InputBox('Ink Spots','Please enter password for ' + sLoginID,'');
  if sLoginPassword <> 'pass'
    then
    begin
      ShowMessage('You shall not pass!');
      //Self.Close;
    end else
      a_Login := True;
 end;
 if a_Login then
   Application.Run;
end;



end.
于 2013-07-23T19:24:43.057 回答