Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Password Program

Hi I'm trying to add a login screen and user accounts to my program.

Basically the variables for the username and password come from an ini file where the variables are defined. This is so the user can edit their username or password. When the user types in a username that is also in the ini file and a password that is also in the ini they will log in.

However when I run my program and the user enters nothing in the Username or Password edit boxes they are logged in. When they enter the correct username and password are defined in the ini files or if they enter anything else they cant log in. Can someone help me I think its my code

Heres the code for the ini File. Its stored in the same folder as the program and is called Password.ini

Password.ini:

[code]
[Users]
Name=Alex
[Pass]
NPass=homepage
[/code]

Heres the code for the program. The parts in bold are the main lines relating to the either the ini File or the user and pass variables.

Code:
[code]
unit Password;

interface

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

//uses iniFiles. The application uses iniFiles as part of the code.

type
TfrmPassword = class(TForm)
ledtUsername: TLabeledEdit;
ledtPassword: TLabeledEdit;
btnOK: TButton;
btnCancel: TButton;
procedure FormCreate(Sender: TObject);
procedure btnOKClick(Sender: TObject);
procedure btnCancelClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
frmPassword: TfrmPassword;
[b]IniFile : TIniFile;[/b] //Defining IniFile as variable TIniFile.
[b]User : String;[/b] //Defining User as a string.
[b]Pass : String;[/b] //Defining Pass as a string.[/b]

implementation

uses Welcome;

{$R *.dfm}

[b]procedure TfrmPassword.FormCreate(Sender: TObject);[/b]
begin
[b]IniFile := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));[/b]
//Associates the ini File 'Password' with the application.
[b]User := IniFile.ReadString('Users','Name','');[/b]
//User equals the variable Name in the users section of the ini file.
[b]Pass := IniFile.ReadString('Pass','NPass','');[/b]
//Pass equals the variable NPass in the Pass section of the ini file.
end;[/b]

[b]procedure TfrmPassword.btnOKClick(Sender: TObject);
begin
if ledtUsername.Text = User
then
if ledtPassword.Text = Pass
then
frmWelcome.show
else
ShowMessage ('Invalid Password.')
else
ShowMessage ('Invalid Username.');
end;[/b]

[/code]

procedure TfrmPassword.btnCancelClick(Sender: TObject);
begin
close;
end;
Any help at all would be great. Thanks

Comments

  • dve83dve83 Member Posts: 30
    I might be wrong, but try it like this (with the begins and ends).

    if ledtUsername.Text = User then
    begin
    if ledtPassword.Text = Pass then
    begin
    frmWelcome.show
    end
    else
    ShowMessage ('Invalid Password.')
    end
    else
    ShowMessage ('Invalid Username.');


    i believe the code

    if condition1 then
    if condition2
    else dosomething
    else dosomethingelse

    isnt being interpreted as you would suspect.

  • dve83dve83 Member Posts: 30
    also note that "component.text = = somestring" would only evaluate to true if that case match as well. For case -insenstive text comparison, try using AnsiComparetext found in strutils.pas
Sign In or Register to comment.