Link to home
Start Free TrialLog in
Avatar of SCOTT78
SCOTT78Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Function and Forms

Hi,

I would like to call a function from form1, allow the user to enter a name and password, perform a check on the info entered and then return a value to form1.
Any ideas on how I can perform this?

I did think about calling a second form from form1, allowing the user to enter details and also performing a check on these details. However, carrying out the procedure in this way, I dont know how I could return a value to form1 where I initially called the second form.

What I am trying to achieve is a security check. The user will click a button to launch a form, but before the form is shown I require the user to enter a name and password. If the user has permissions to access this form (permissions will be setup for each form), then the form will be shown else the user will receive a message informing of no permission to view the form.

Any suggestions would be greatly appreciated.
Thanks
Sct
Avatar of mokule
mokule
Flag of Poland image

In main Form
procedure TFMain.FormActivate(Sender: TObject);
begin
      Application.CreateForm(TFLogin, FLogin);
      if (FLogin.ShowModal <> mrOK) then
         Close;
      FLogin.Free
end;


In Login Form
Assign
ModalResult := mrOK // when Your checking is OK
something else otherwise
"I would like to call a function from form1, allow the user to enter a name and password, perform a check on the info entered and then return a value to form1." This is a simple Function call expl:

function myf(s,p:string):string;
begin
if s='User' and p='Password' then myf:='OK' else myf:='NotOK';
end;

begin
........
If myf(Form1.Edit1.Text, Form1.Edit2.Text)='OK'
then
//user is allowed to view the form
SecretForm.ShowModal;
else
ShowMessage('You are not allowed to view this form');
.......
end;
Avatar of shaneholmes
shaneholmes

Create a login form. In my example I call it frmSecurity.

Set its border style to bsDialog.

place two Tlabels , 2 TEdits, and a TButton.

set the 1st label's caption = 'UserName'
set the 2nd label's caption = 'Password'

set the 2nd TEdit's PasswordChar property  = '*'

Set the TButtons caption to 'OK' and its ModalResult = mrOK


in the form's OnCLoseQuery event do the following

procedure TfrmSecurity.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
 if ValidateInfo(edtUserName.Text, edtPassword.Text) then
  modalResult:= mrOK
 else
  modalResult:= mrCancel
end;

//here is the ValidateInfo function

function TfrmSecurity.ValidateInfo(UserName, Password: String): Boolean;
begin
 //compare your username & password here
 result:= (UserName = gUserName ) AND (Password = gPassword);
end;


//you will need to replace my gUserName & gPassword with your variables.

below is the entire security form unit code
______________________________________________________________

unit UntSecurity;

interface

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

type
  TfrmSecurity = class(TForm)
    edtUserName: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    edtPassword: TEdit;
    Button1: TButton;
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  private
    { Private declarations }
    function ValidateInfo(UserName, Password: String): Boolean;
  public
    { Public declarations }
  end;

var
  frmSecurity: TfrmSecurity;
  gUserName: String = 'Test1';
  gPassword: String = 'Test2';

implementation

{$R *.dfm}

function TfrmSecurity.ValidateInfo(UserName, Password: String): Boolean;
begin
 //compare your username & password here
 result:= (UserName = gUserName ) AND (Password = gPassword);
end;

procedure TfrmSecurity.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
 if ValidateInfo(edtUserName.Text, edtPassword.Text) then
  modalResult:= mrOK
 else
  modalResult:= mrCancel
end;

end.
________________________________________________________________________



OK, if you have a button in your main form, do this:

procedure TForm1.Button1Click(Sender: TObject);
begin
 if frmSecurity.ShowModal = mrOK then
  frmSub.Show; //show the form you wanna show
end;


Hope this helps!

Shane
If you are trying to return a specific value from Form2, rather than just a success or failure indicator, you can define a public variable in the interface section of Form2, and reference it in your call to Form2 from Form1, like so:

TForm2 = class(TForm)
    Btn_ok: TBitBtn;
    procedure Btn_okClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    SpecialValue : string;
  end;

then in Form1:

with TForm2.Create( Application ) do
try
   if ( ShowModal = mrOK ) then
      sValue := SpecialValue;
finally
   Release;
end;
I always use a "class function" for this kind of call.

Memo1.Lines.Add(TfrmLogin.Run(Self, 'NONE'));

This would add the successfully authenticated user to the Memo.

Regards Jacco

P.S:
- The function returns '' if no user was authenticated
- The 'NONE' passed could be replaced by the last logged in user or the username of the user logged on to windows

<<< Start of Unit2.pas >>>
unit Unit2;

interface

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

type
  TfrmLogin = class(TForm)
    btnOK: TButton;
    btnCancel: TButton;
    edName: TEdit;
    lbName: TLabel;
    lbPassword: TLabel;
    edPassword: TEdit;
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  public
    Tries: Integer;
    class function Run(aOwner: TComponent; aUser: string = ''): string;
  end;

implementation

{$R *.dfm}

{ TForm2 }

class function TfrmLogin.Run(aOwner: TComponent; aUser: string): string;
begin
  with TfrmLogin.Create(aOwner) do
  try
    edName.Text := aUser;
    if ShowModal = mrOK then
      Result := edName.Text
    else
      Result := '';
  finally
    Free;
  end;
end;

procedure TfrmLogin.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  if ModalResult = mrOK then
  begin
    CanClose := False;
    if (edName.Text = 'USER') and (edPassword.Text = 'PASS') then
      CanClose := True
    else
    begin
      Inc(Tries);
      if Tries = 3 then
      begin
        CanClose := True;
        ModalResult := mrAbort;
        ShowMessage('Bye bye');
      end else
        ShowMessage('This is a wrong combination try again');
    end;
  end;
end;

end.
<<< End of Unit2.pas >>>
<<< Start of Unit2.dfm >>>
object frmLogin: TfrmLogin
  Left = 389
  Top = 233
  ActiveControl = edPassword
  BorderStyle = bsDialog
  Caption = 'Login'
  ClientHeight = 109
  ClientWidth = 219
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCloseQuery = FormCloseQuery
  PixelsPerInch = 96
  TextHeight = 13
  object lbName: TLabel
    Left = 8
    Top = 8
    Width = 28
    Height = 13
    Caption = '&Name'
    FocusControl = edName
  end
  object lbPassword: TLabel
    Left = 8
    Top = 40
    Width = 46
    Height = 13
    Caption = '&Password'
    FocusControl = edPassword
  end
  object btnOK: TButton
    Left = 54
    Top = 74
    Width = 75
    Height = 25
    Caption = 'OK'
    Default = True
    ModalResult = 1
    TabOrder = 2
  end
  object btnCancel: TButton
    Left = 134
    Top = 74
    Width = 75
    Height = 25
    Cancel = True
    Caption = 'Cancel'
    ModalResult = 2
    TabOrder = 3
  end
  object edName: TEdit
    Left = 64
    Top = 8
    Width = 145
    Height = 21
    TabOrder = 0
  end
  object edPassword: TEdit
    Left = 64
    Top = 40
    Width = 145
    Height = 21
    PasswordChar = '*'
    TabOrder = 1
  end
end
<<< End of Unit2.dfm >>>
Avatar of SCOTT78

ASKER

At present I am using this code to call my form:

procedure TFMain.FormActivate(Sender: TObject);
begin
      Application.CreateForm(TFLogin, FLogin);
      if (FLogin.ShowModal <> mrOK) then
         Close;
      FLogin.Free
end;

Is there a way I can pass a string to the form as I want to be able to send the name of the form that the user is presently on.

Thanks
Sct
ASKER CERTIFIED SOLUTION
Avatar of mokule
mokule
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial