Reputation: 97
i obtained an example on how to create a login screen before the main form is created. Howwever i do not know how to obtain the variable before the login screen closes. I am trying to pass the variable
SelectedUserName : String;
SelectedUserIdNo, SelectedCoyId : Integer;
from the loginfrm to the mainform for further processing.
any ideas.
thanks in advance.
here is main code:
program Pac;
{$R *.res}
uses
ExceptionLog, Forms,
MainForm in 'Main\MainForm.pas' {MainFormFrm} ,
Datamodule in 'Main\Datamodule.pas' {DataModuleFrm: TDataModule} ,
Login in 'Security\Login.pas' {LoginFrm};
begin
if tLoginFrm.Execute then
begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TMainFormFrm, MainFormFrm);
Application.CreateForm(TDataModuleFrm, DataModuleFrm);
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.
My Login code is :
unit Login;
interface
uses
Windows, .. .. ..;
type
TLoginFrm = class(TForm)
Label1: TLabel;
ButtOk: TButton;
ButtCancel: TButton;
cxMaskEditUserId: TcxMaskEdit;
cxMaskEditPw: TcxMaskEdit;
ButtReset: TButton;
Label2: TLabel;
QueryUser: TMSQuery;
MSConnectionMain: TMSConnection;
procedure ButtOkClick(Sender: TObject);
procedure CheckMenuAccess;
procedure ButtResetClick(Sender: TObject);
procedure FormShow(Sender: TObject);
public
SelectedUserName: String;
SelectedUserIdNo, SelectedCoyId: Integer;
{ Public declarations }
class function Execute: boolean;
end;
implementation
uses DataModule, MainForm, OutletListing;
{$R *.dfm}
class function TLoginFrm.Execute: boolean;
begin
with TLoginFrm.Create(nil) do
try
Result := ShowModal = mrOk;
finally
Free;
end;
end;
procedure TLoginFrm.FormShow(Sender: TObject);
begin
MSConnectionMain.Connected := True;
end;
procedure TLoginFrm.ButtOkClick(Sender: TObject);
begin
{ Verify users are in list of users }
With QueryUser Do
Begin
Active := False;
if cxMaskEditUserId.EditValue = Null then
ParamByName('UserId').Clear
ELSE
ParamByName('UserId').AsString := cxMaskEditUserId.EditValue;
if cxMaskEditUserId.EditValue = Null then
ParamByName('Userpassword').Clear
ELSE
ParamByName('Userpassword').AsString := cxMaskEditPw.EditValue;
Active := True;
If (FieldByName('UserId').IsNull) or
(cxMaskEditUserId.EditValue = Null) Then
Begin
cxMaskEditUserId.EditValue := Null;
cxMaskEditPw.EditValue := Null;
cxMaskEditUserId.SetFocus;
End
Else
Begin
OutletListingFrm := TOutletListingFrm.Create(Self);
SelectedUserIdNo := FieldByName('UserIdNo').AsInteger;
SelectedUserName := FieldByName('UserName').AsString;
OutletListingFrm.SelectedUserId := FieldByName('UserIdNo').AsInteger;
IF OutletListingFrm.ShowModal = mrOk THEN
BEGIN
SelectedCoyId := FieldByName('CoyId').AsInteger;
ModalResult := mrOk;
END
ELSE
ModalResult := mrCancel;
OutletListingFrm.Free;
End;
End;
end.
Upvotes: 3
Views: 319
Reputation: 613432
Create a record containing the information to be returned from the login form:
type
TLoginInfo = record
SelectedUserName: string;
SelectedUserIdNo: Integer;
SelectedCoyId: Integer;
end;
Then return such a record from the Execute
method of the login class:
function Execute(out LoginInfo: TLoginInfo): Boolean;
If the login is successful, then the implementation of the Execute
method needs to fill out these details.
Then pass the information to the main form. You cannot do that in the call to Application.CreateForm
. So instead you'd need a different method on TMainFormFrm
that can be called after the main form has been created. And that method would receive the TLoginInfo
record returned from the successful login.
So to TMainFormFrm
you would add a public method named InitialiseWithLoginInfo
, say.
procedure InitialiseWithLoginInfo(const LoginInfo: TLoginInfo);
Then your .dpr file would look like this:
var
LoginInfo: TLoginInfo;
begin
if tLoginFrm.Execute(LoginInfo) then
begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TMainFormFrm, MainFormFrm);
MainFormFrm.InitialiseWithLoginInfo(LoginInfo);
Application.CreateForm(TDataModuleFrm, DataModuleFrm);
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.
Upvotes: 7