Solved

Function and Forms

Posted on 2004-04-02
7
252 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

749 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