Rijnhardt
Rijnhardt

Reputation: 2724

Terminate the creation of form before form is active

I want the user to log in before he/she would be able to view the form...

It does not seem to work.

Any ideas?

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

;

Upvotes: 2

Views: 261

Answers (2)

Rob Kennedy
Rob Kennedy

Reputation: 163247

If the form isn't meant to be created, then it should throw an exception from its constructor. That's the defined way to avoid creation of an object. Note that OnShow and OnCreate are not the constructor; you'll need to override Create instead.

In your case, you're attempting to solve the problem in the wrong place. The better way to avoid creating a form that you don't really want is to never create it in the first place. Instead of creating your form and then checking whether it's allowed, check whether it's allowed first, and then show it.

You can wrap that operation into a function to keep things easy for the caller. For example:

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;

Then, you can use that method to create your form, but only if the user is correct and the password is entered twice.

RijnhardtForm := TRijnhardtForm.ConditionallyCreate;

Upvotes: 10

House of Dexter
House of Dexter

Reputation: 386

In your project source...

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.

Then change your login to a procedure

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.

Upvotes: 0

Related Questions