Delhpi - transfering text to string?

sitnet

Senior Member
Joined
Apr 4, 2008
Messages
886
Reaction score
34
Hello :)

Ok so this is the problem, I am writing a basic program in Delphi 7 and need some help. Basically the program is a unit converter (Like currencies and speed etc.)

There are a few things I need help with:

1) For my program I need to have a login form. Currently usernames and passwords are being stored in 2 listboxes, one for the names and the other for the passwords. I would like to rather save these usernames and passwords in a textfile.

I have already written the code on form activation to to assign 2 text files. Now how I want this to work is that on program startup all the entries in the users.txt must be carried over to listbox 1 and all the entries in the passwords.txt must be carrried over to the listbox 2. And when a new user is registered the username must be transfered to the users.txt and the password to the password.txt file.

How can I achieve this?

BTW my current Login procedure looks like this:

Code:
procedure TMainForm.LoginbtnClick(Sender: TObject);
var
amount   : integer;
S        : integer;

begin
amount   := 0;
S        := -1;
loggedin := false;


Repeat

begin;
amount := amount + 1;
S      := S + 1;
IF (LoginName.Text = LoginNameList.Items[S]) and (Loginpass.Text = LoginPassList.items[S]) THEN

begin
Showmessage('Thank you for logging in');
MainPage.Visible := true;
LoginPanel.Visible := False;
LoggedIn := true;
Logoffbtn.Visible := true;
exit;
end

end;

Until amount = 5;

IF loggedin = false THEN
begin
Showmessage('Invalid login details entered, please try again');
exit;
end;
 
Rather have one file as its easy for the two to get out of sync. Also do not forget to SALT the password else anybody can just access the file with plain text usernames and passwords. But in all honesty your doing it wrong, you should be working from a Database instead - even SqlLite would be perfect.

Edit: What Delphi7 you using? Does it have the DBExpress plugins/applets??
 
+1 to database
much easier than textfiles
i got more marks for my database one in matric pat
than my friend who used text files
our programs were similar enough
 
My vote goes for DB as well.

If you want to use the ListBox.... here it is.

Code:
ListBox1.Items.SaveToFile(theFileName);
ListBox1.Items.LoadFromFile(theFileName);
 
If you are hell bent on using a flat file, might I suggest you use an INI file to do the heavy lifting for you. Using database would be first prize, but I assume you'res starting out, so baby steps FTW!
You'll need to and the IniFiles namespace to your uses in your class/form.

I'm just going to copy past what I threw together as I'm kinda pressed for time. I hope you'll read through it and digest what I've done. Which by no means is the best bit of code I've bashed out.

The forms consists of two TEdit, two TButton and a TListBox

SIDENOTE: Awesome to know that people are still learning Object Pascal (Delphi), it's still my first love when it comes to programming languages.

Code:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    txtName: TEdit;
    txtPassword: TEdit;
    btnRegister: TButton;
    lstBoxUsers: TListBox;
    btnUsers: TButton;
    btnLogin: TButton;
    procedure btnRegisterClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure btnUsersClick(Sender: TObject);
    procedure btnLoginClick(Sender: TObject);
  private
    { Private declarations }
    MyIni : TIniFile;

    function EncodeMyString(const AString : String): String;
    function DecodeMyString(const AString : String): String;

    procedure LoginFailed();
    procedure LoginSuccess(const AUserName : String);
  public
    { Public declarations }

  end;

var
  Form1: TForm1;

implementation


{$R *.dfm}
procedure TForm1.btnUsersClick(Sender: TObject);
var
  i : Integer;
  slUser : TStringList;
begin
  slUser := TStringList.Create;
  try
    MyIni.ReadSections(slUser);
    lstBoxUsers.Items.Assign(slUser);
  finally
    FreeAndNil(slUser);
  end;
end;

function TForm1.DecodeMyString(const AString: String): String;
begin
  // Undo Your Magic
  Result := AString;
end;

function TForm1.EncodeMyString(const AString: String): String;
begin
  // Make Your Magic
  Result := AString;
end;

procedure TForm1.btnLoginClick(Sender: TObject);
begin
  if MyIni.SectionExists(txtName.Text) then
  begin
    if txtPassword.Text = DecodeMyString(MyIni.ReadString(txtName.Text, 'Password', '')) then
    begin
      LoginSuccess(txtName.Text);
    end
    else
    begin
      LoginFailed();
    end;
  end
  else
  begin
    LoginFailed();
  end;
  
end;

procedure TForm1.btnRegisterClick(Sender: TObject);
begin
  MyIni.WriteString(txtName.Text, 'Password', EncodeMyString(txtPassword.Text));
  MyIni.UpdateFile;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  MyIni := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini'));
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  MyIni.UpdateFile;
  FreeAndNil(MyIni);
end;

procedure TForm1.LoginFailed;
begin
  MessageDlg('Login Failed', mtWarning, [mbOK], 0);
end;

procedure TForm1.LoginSuccess(const AUserName : String);
begin
  MessageDlg('Welcome Back ' + AUserName, mtInformation, [mbOK], 0);
end;

end.
 
Yeah, I would go with the INI file but encrypt the passwords before you write them to the file. I am on the delphi forums at DevShed helping newbs... and believe me - they can be stubborn :D Delphi is awesome. I've done them all from Turbo Pascal and it only gets better.
 
Would be great to hear from OP if he's made anymore progress. As for getting to grips with database development the fellow at :http://delphi.about.com
Has a great series of tutorial for first timers.
 
Top
Sign up to the MyBroadband newsletter
X