Solved

Function and Forms

Posted on 2004-04-02
7
241 Views
Last Modified: 2010-04-05
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
0
Comment
Question by:SCOTT78
7 Comments
 
LVL 17

Expert Comment

by:mokule
ID: 10739406
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
0
 
LVL 11

Expert Comment

by:calinutz
ID: 10739425
"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;
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10740573
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
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 1

Expert Comment

by:wframsay
ID: 10740593
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;
0
 
LVL 10

Expert Comment

by:Jacco
ID: 10747097
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 >>>
0
 

Author Comment

by:SCOTT78
ID: 10773034
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
0
 
LVL 17

Accepted Solution

by:
mokule earned 125 total points
ID: 10773160
1. If You declare for example
SomeString: string;
 in form FLogin in section public You can have access to it by

FLogin.SomeString := 'xxxx';

2. If You want to present some text in Label or Edit You have access to it by

FLogin.Label1.Caption := 'xxxx';
or
FLogin.Edit1.Text := 'xxxx';

Label1 or Edit1 are the controls on form FLogin

All this is valid after
      Application.CreateForm(TFLogin, FLogin);
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now