Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Function and Forms

Posted on 2004-04-02
7
Medium Priority
?
272 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 500 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

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…
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 video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Screencast - Getting to Know the Pipeline
Suggested Courses

610 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