As part of my A-Level computing coursework i have to simulate an end user and design a programe for them.

I have the code to allow for a listbox were you select from a list of usernames you want but for added security i want to ammend it so you have to type in both the username and password (in text boxes) not just select one of the optional usernames (in the listbox). the code i have that works for the list box is as follows. Can anyone tell me how i can change my code so that you must type in the username?

for easy reading the username box is called TxtUsername and the password box is TxtPasword

thanks in advanced

my code is as follows:

unit RSPCA;

interface

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

type
  TFrmRSPCALogin = class(TForm)
    BtnLogin: TButton;
    BtnExit: TButton;
    TxtPassword: TEdit;
    TxtUsername: TEdit;
    Image1: TImage;
    LblUsername: TLabel;
    Label1: TLabel;
    procedure BtnExitClick(Sender: TObject);
    procedure BtnLoginClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FrmRSPCALogin: TFrmRSPCALogin;

implementation


type

  Tuser = record
    s_Username  : string;
    s_Password  : string;
    i_type      : integer;

end;

const
  USERS     = 'Users.txt';
  SETTINGS  = 'Settings.txt';

var
  a_user      : array [0..15] of TUser;
  i_logins    : integer;
  i_maxlogins : integer;

{$R *.dfm}

procedure TFrmRSPCALogin.BtnExitClick(Sender: TObject);
begin
  Application.Terminate;
end;

procedure TFrmRSPCALogin.BtnLoginClick(Sender: TObject);
var
  fp_settings : textFile;
begin
  inc(i_logins);

  if i_logins > i_maxlogins then
  begin
    messagedlg('To many Login Attempts, Program Is Now Locked!', mtError, [mbOk], 0);

    AssignFile(fp_settings, SETTINGS);
    Rewrite(fp_settings);

    WriteLn(fp_settings, '1');
    WriteLn(fp_settings, InttoStr(i_maxlogins));

    CloseFile(fp_settings);

    Application.terminate;
    end else
    begin
      if Uppercase(trim(TxtPassword.Text)) =
      Uppercase(trim(a_User[TxtUsername.ItemIndex].s_password)) then
        begin
          s_username  := a_user[LstUsers.Itemindex.s_Username;
          FrmPassword.Hide;
          Application.CreateForm(TFrmMain, FrmMain);
          FrmMain.Show;
          end else
          begin
          MessageDlg('Wrong Password!', mtError, [mbOk], 0);
          TxtPassword.Clear;
          TxtPassword.setFocus;
          end;
        end;
    end;
procedure TFrmRSPCALogin.FormCreate(Sender: TObject);
var
  fp_users    : Textfile;
  fp_settings : Textfile;
  i_users     : integer;
  s_temp      : string;

  begin
    if FileExists(SETTINGS) then
    begin
        AssignFile(fp_settings, SETTINGS);
        reset(fp_Settings);

        ReadLn(fp_settings, s_temp);
        if s_temp <> '0' then
        begin
          MessageDlg('Program is Locked!', mtError, [mbOk], 0);
          Application.terminate;
        end else
          begin
            ReadLn(fp_settings, s_temp);
            i_maxlogins := StrToInt(s_temp);
          end;
        CloseFile(fp_settings);
    end else
      begin
        MessageDlg('Missing Settings Files!', mterror, [mbOk], 0);
        Application.terminate;
      end;
    if FileExists (USERS) then
    begin
      AssignFile(fp_users, USERS);
      Reset(fp_USERRS);

      i_users := 0;

      while not EOF(fp_users) do
      begin
      ReadLn(fp_Uers, a_user[i_users].s_username);
      LstUsers.items.Add(a_user[i_users].s_username);
      ReadLn(fp_Users, a_user[i_users].s_password);

      ReadLn(fp_users, s_temp);
      a_User[i_users].i_type := StrToInt(s_temp);

      inc(i_users);
    end;

    closeFile(fp_users);
    end;i_logins :=0;

  end else
  begin;
  MessageDlg('Password File does not exist!', mtError, [mbOk], 0);
  Application.terminate;
  end;
end;



end.

I'm not sure of your intent. I visualize three compnents on the form, the list box with the user names, and two text boxes, one for names and one for passwords. IF you have a listbox that has read the nnames, then it is a simple matter for the user to copy it over to the textbox. Are you trying to keep the list of names private or visible? Please expand upon what you are attempting to do here

Change it to a text box, and then as part of the check, check that the contents of that box is in the valid usernames list.

I'm not sure of your intent. I visualize three compnents on the form, the list box with the user names, and two text boxes, one for names and one for passwords. IF you have a listbox that has read the nnames, then it is a simple matter for the user to copy it over to the textbox. Are you trying to keep the list of names private or visible? Please expand upon what you are attempting to do here

on my login screen i have 2 boxes, both text boxes one for username one for password, how ever the code given to me to modify to fit my own form names etc by my teacher was that of a login screen using a list box were you just select the username and enter the password. i want it so you have NO list box but in its place have an edit box were you manually enter the username

I think I answered your question. You just chose to ignore me.

i didnt ignore you, what you said jsut wasnt to helpful to me, please bare in mind im a student and im not great at programming to start with. the line i seem to be getting most of my problems on is

"Uppercase(trim(a_User[TxtUsername.ItemIndex].s_password)) then"

i still have no idea how to fix it or what to type in place of the rest of the code.

as you can tell my teacher isnt the best ^^

Or as a student, perhaps you arent asking him the right questions, as with me, you didnt say "oh, I dont see how I'd do that" you just ignored it.. perhaps you do the same to him?

Anyway.

Do you have understanding as to what the line

Uppercase(trim(a_User[TxtUsername.ItemIndex].s_password))

does?

to my knowledge, if im correct that line should allow the program to ignore differences between upper and lower case characters in the password how ever when the code was given to me i did previously have this

Uppercase(trim(a_User[LstUsername.ItemIndex].s_password))

the difference being ive changed it from "LstUsername" to "TxtUsername" although i tried changing it back and adding in the list box with that name instead and it still didnt work ^^

So you dont even know what the line does then..

I would suggest you go back to your teacher, as that line of code is far from complex if you have failed to understand, either the guy is a complete moron, or you didnt listen. Or some percentage of the 2.

If you changed the TxtUserName (and assuming its an editbox) then that code shouldnt have compiled as editboxes dont have an itemindex.

If you struggle with something as simple as breaking down some code you were given try working through this free online training course

Its not perfect but it should teach you more than you seem to already know

i know something is wrong with that line but the teacher i have jsut says, to do what you want will require different code... which i know but he wont help me write the different code, hence why im here. i know that itemindex is wrong, i pretty much figured it was that phrase there to start with. and if that line doesnt do what i stated then its my teacher who is wrong as thats what i was told.

rather than beating around the bush if you can see whats wrong with it could you not be less pedantic and correct it for me? save us all the hassle. after all that is why i posted this thread, to get help correcting it.

Im not being pedantic. You obviously dont know what the code you had did, someone must have given it to you, if your teacher wont teach you, and hes paid to do so, why should I? Ive linked you a training course, did you look at it? by the sounds of it no.

Im not being pedantic, no one can be there to do everything for you, and countless times Ive seen people go "ooh but I only learn if someone shows me the answer" only to watch them ask the same question again days/weeks later.. Its a waste of my time handing out answers, its better use of my time (although more frustrating for me) to try and help you help yourself.

First thing you need to do is work out what that line of code does.

Once you know what it does, you can change it.

Until then, you're not going to understand anyway

the point im gettin at is, i dont know the phrasing of what it is i need to write, in my users file i have the line

username, password, access level

i just need help writing code so that it will read that file and find if any of the usernames in it match what has been typed in the username edit box. if so then check password if that matches then continue to logging in.

Then read the information I gave, you you will work out what the code line does, and have an answer

This article has been dead for over six months. Start a new discussion instead.