Board index » delphi » Detecting Cancel button click on Login dialog for TDatabase

Detecting Cancel button click on Login dialog for TDatabase

Anyone know of an easier way to tell if the "Cancel" button was pressed in
the database login dialog box (default) that is displayed when you try to
"Connect" to a TDataBase?

I wanted to give the user 3 tries to get the password right, and exit the
app if they fail all three times.  Also, if they press Cancel, I want to
bail out right there too.

All I could find to work with is that the wrong password error gives a
different exception (EDBEngineError) than the exception raised when the user
clicks the Cancel button (EDatabaseError).  Not much to go on, as there are
a lot of other error conditions that could throw those same exceptions.
And, I don't really want to go by the text of the exception message, as that
could be changed in the future.  Is there any other way to tell if the user
clicked the Cancel button on the default TDataBase Login dialog?

  Result := TDatabase.Create(nil);
  with Result do begin
    AliasName := GetDatabaseName(asAppExeName);
    DatabaseName := AliasName;
    try
      Connected := True;
    except
      {If bad password, try again}
      on e: Exception do begin
        if e is EDBEngineError then begin
          {Bad password supplied, give user a second chance at the logon}
          try
            Connected := True;
          except
            on e: Exception do begin
              if e is EDBEngineError then begin
                {Bad password again! Give user a third (last) chance to
logon}
                try
                  Connected := True;
                except
                  if e is EDatabaseError then begin
                    {User pressed Cancel button, do nothing, return to
caller}
                  end
                  else begin
                    {3rd login failure, do nothing, just return to caller}
                  end;
                end;
              end
              else begin
                if e is EDatabaseError then begin
                  {User pressed Cancel button}
                end
                else begin
                  {Re-raise the exception}
                  raise;
                end;
              end;
            end; {on..do}
          end;
        end
        else begin
          if e is EDatabaseError then begin
            {User pressed Cancel button}
          end
          else begin
            {Re-raise the exception}
            raise;
          end;
        end;
      end; {on..do}
    end; {try..except}
  end; {with Result}

 

Re:Detecting Cancel button click on Login dialog for TDatabase


Quote
> Anyone know of an easier way to tell if the "Cancel" button was pressed in
> the database login dialog box (default) that is displayed when you try to
> "Connect" to a TDataBase?

Don't bother with the default box.  Set LoginPrompt to False and use your
own dialog box.  In your tDatabase component add 'user name=abc' and
'password=xyz' to the params strings.

-Mike

Re:Detecting Cancel button click on Login dialog for TDatabase


You can write your own handler for OnLogin event and use the function
LoginDialogEx to get the username and password.  Then you can have control
on how many time the user can try to logon.  Why don't you refer to the
Delphi source code to find out how Delphi do it.  It is very simple.  You
can find the source in TDatabase.Login procedure in the dbTables.pas.  The
file is under Delphi's source/vcl;

Ping

Quote
Mike Williams <mi...@remove.aps-soft.com> wrote in message

news:8bh1c0$d8214@bornews.borland.com...
Quote
> > Anyone know of an easier way to tell if the "Cancel" button was pressed
in
> > the database login dialog box (default) that is displayed when you try
to
> > "Connect" to a TDataBase?

> Don't bother with the default box.  Set LoginPrompt to False and use your
> own dialog box.  In your tDatabase component add 'user name=abc' and
> 'password=xyz' to the params strings.

> -Mike

Re:Detecting Cancel button click on Login dialog for TDatabase


Yes, looking at the Delphi source code seems like just the ticket!
Sometimes I forget to use this resource.
Thanks!
Quote
Ping Kam wrote in message <8bh2rt$d...@bornews.borland.com>...
>You can write your own handler for OnLogin event and use the function
>LoginDialogEx to get the username and password.  Then you can have control
>on how many time the user can try to logon.  Why don't you refer to the
>Delphi source code to find out how Delphi do it.  It is very simple.  You
>can find the source in TDatabase.Login procedure in the dbTables.pas.  The
>file is under Delphi's source/vcl;

Other Threads