Save problem

Hey ill just paste here my code
The issue with it is that it saves random parts of the file it doesnt save passwor and username seperately help plz
unit Users;


interface

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

type
TFrmUsers = class(TForm)
Label1: TLabel;
Label2: TLabel;
TxtUser: TEdit;
TxtPassword: TEdit;
Btnprevious: TButton;
BtnNew: TButton;
BtnSearch: TButton;
Btndelete: TButton;
BtnNext: TButton;
PnlStatus: TPanel;
BtnExit: TButton;
procedure FormCreate(Sender: TObject);
procedure BtnNextClick(Sender: TObject);
procedure BtnpreviousClick(Sender: TObject);
procedure TxtUserChange(Sender: TObject);
procedure TxtPasswordChange(Sender: TObject);
procedure BtnNewClick(Sender: TObject);
procedure BtnExitClick(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure BtndeleteClick(Sender: TObject);
procedure BtnSearchClick(Sender: TObject);
private
procedure showrecord;
procedure updatepanel;
public
{ Public declarations }
end;

var
FrmUsers: TFrmUsers;

implementation

{$R *.dfm}

type
TUser = record //new data type declaration
s_username : string; //new data type declaration
s_password : string; //new data type declaration

end;

var
a_user : array[0..99] of Tuser; //New var char array = space in ram reserved
i_noofusers : integer; //number of users integer must be a whole number
i_currentuser : integer; // whats the current user we are looking at
b_changed : Boolean ; //logic topic of variable flase or true
Const
FILENAME = 'users.txt'; (*Creates a name for our users.txt that is going
to be uses in delphi *)

procedure TFrmUsers.BtndeleteClick(Sender: TObject);(*after clicking the delete
button *)
var
i_counter : integer; //creates a new variable called
begin
if messagedlg('Are you sure', mtconfirmation, [mbNo, mbYes],
0)= mrYes then //if statement after clicking delete asks for a confirmation
begin
if i_noofusers = 1 then //if there is 1 user then...
begin
messagedlg ('You cant delete the last user', mterror,
[mbOk], 0); //send a message that this cannot be deleted


end else //if there is more than 1 user
begin
for I_counter := i_currentuser to (i_noofusers - 1) do (*loop for assigns
that i counter must be i current user to the last user in the array*)
begin
a_user[i_counter] := a_user[i_counter + 1]; (* it means that whatever is
in the 3rd record will be copied over to the second record as many times
as i_counter tell it to*)

end;

if i_currentuser = (i_noofusers -1) then (*an exceptien if we are deleting
the last record then we just dec the noofuser which will atuomaticly wipe
the last record*)
dec(i_currentuser);

dec(i_noofusers);

showrecord;

end;
end;

end;

procedure TFrmUsers.BtnExitClick(Sender: TObject); (*when we click at the button
exit it just closes*)
begin
Close;
end;

procedure TFrmUsers.BtnNewClick(Sender: TObject); //after clioking at new button
begin
a_User [i_noofusers] .s_username := ''; //shows a blank text box
a_user [i_noofusers] .s_password := ''; //shows a blank text box

i_currentuser := i_noofusers; // displays the new blank record

inc(i_noofusers); //adds a new user
showrecord;
TxtUser.SetFocus;

end;

procedure TFrmUsers.BtnNextClick(Sender: TObject); //After clicking at next btn
//no need for message dlg code since we cant click it when its the last user
begin

inc(i_currentuser); //adds +1 to the current user displays next
showrecord;
end;




procedure TFrmUsers.BtnpreviousClick(Sender: TObject);


begin

dec(i_currentuser); //decreases the i_currentuser and shows the previous record
showrecord;
end;


procedure TFrmUsers.BtnSearchClick(Sender: TObject); //search button code
var
s_searchstring : string; //searchstring string of letters
b_found : Boolean; //found flag logic unit false or true
i_counter : integer; //declares i counter again?!
begin
s_searchstring := Trim(uppercase(inputbox('Search',
'Enter a user name :-):-)','')));
(*Shows labels and tells the user what to write where also tells processor to
convert low case to uppercase and ignore spaces infront and at the end*)

//It sets the flag to false and the icounter to
if length(s_searchstring)> 0 then (*if there is something written in the
the text box then start searching*)
begin
b_found := false; //sets the found flag to false
i_counter := 0; //resets the counter to 0 so it starts from first record



while(b_found = false) and (i_counter < i_noofusers) do (*if found flag is
still false and the i counter is lesser than total number of users carry on
doing this loop*)
begin
if s_searchstring = Trim(uppercase(a_user[i_counter].s_username))(*
if the text in searchstring = text in any array converted to capitals
then do following*)
then
begin
b_found := True; //Set the flag to true which will stop the loop
i_currentuser := i_counter; //displays the found record

Showrecord;
end else
begin
inc(i_counter); (*if we havent found it while scanning the first
record then keep on scanning next and the next one until we reach
noofusers which is the last record or the found flag is true*)
end ;


end;
if b_found = false then
messagedlg ('User not found', mterror,
[mbOk], 0); (*if after total scan found flag is still false desplay this
message*)
end;




end;

procedure TFrmUsers.FormCloseQuery(Sender: TObject; var CanClose: Boolean);(*
close query means that if a user tries to close in any way the following happens
even alt ctr delete or shutting down the computer*)
var
fp_users :textfile;
i_counter : integer; //again i counter declared
begin
if messageDlg('Are you sure', mtconfirmation,
[mbYes, mbNo], 0) = MrYes then //asks whether a user is sure about closing
begin
Canclose := true; //sets the close flag to true
if b_changed = True then //if changed flag is true then
begin
if messagedlg('Do you want to save changes',
mtconfirmation,[mbYes, mbNo], 0)= mrYes then (*if a user wants to save
changes then it does the following*)
begin
AssignFile(fp_users, FILENAME); //states that fp_users = filename
Rewrite(fp_users); //rewrites fp_users with

for i_counter := 0 to (i_noofusers - 1) do
begin (* a loop that does following
from the first record in array to the last user which is the last user
in the array because it count from 0 instead of 1*)

Writeln(fp_users, a_user[i_counter].s_username); (*writes to the file
fp)users whatever is in the array user in the textboxusername*)
Writeln(fp_users, a_user[i_counter].s_password);(*writes to the file
fp)users whatever is in the array user in the textboxpassword*)
Closefile(fp_users);
end;
end;

end;



end else
begin
Canclose := False; //if a user clicked on no program cancels close file
end;
end;

procedure TFrmUsers.FormCreate(Sender: TObject);
var
fp_users : TextFile; //declares that fp_users is a textfile
begin
if FileExists (FILENAME) then //if the program can find the file then
begin
AssignFile(fp_Users, FILENAME); //it assigns file users to go to filename
Reset(fp_users); //wipes fp users
i_noofusers := 0; // sets the noofusers to 0

while not EOF(fp_users) do //while loop EOF= end of record
begin
Readln(fp_users, a_User[i_noofusers].s_username) ; (* loads whatever
is in the text file into fp users into given arra to text box username*)
Readln(fp_users, a_User[i_noofusers].s_password) ; (* loads whatever
is in the text file into fp users into given arra to text box password*)

inc(i_noofusers); //after doing so to pass and username it goes to another record
end;


CloseFile(fp_users); //after reading till the last user it closes file

i_currentuser := 0; //displays the first record

showrecord;

end else
begin
MessageDlg('The file does not exist', mtError, [mbOK], 0);
Application.Terminate; //if the file does not exist it shows a message

end;
end;

procedure TFrmUsers.TxtPasswordChange(Sender: TObject);
begin
if TxtPassword.Text <> a_user[i_currentuser].s_password then (* if text in the
text file doesnt equal whats currently in the text then *)
begin
a_user[i_currentuser].s_password := TxtPassword.Text; (* puts anything that
is in the ram currently into the txtpassword*)
b_changed := True; //flags the changed to true
end;
end;

procedure TFrmUsers.TxtUserChange(Sender: TObject);
begin

if txtUser.Text <> a_user[i_currentuser].s_username then
begin a_user[i_currentuser].s_username := TxtUser.Text;
b_changed := True
//same thing happens here that happens in the txt password change wirh some minor chanegs
end;
end;

procedure TFrmUsers.showrecord; //whole code for procedure show record
begin
TxtUser.Text := a_user[i_currentuser].s_Username; (*shows username
of the current user*)
TxtPassword.Text := a_user[i_currentuser].s_Password; (* shows password
of the current user*)
if i_currentuser = 0 then Btnprevious.Enabled := False
else Btnprevious.Enabled := True; (* if the record we are currently
looking at is the first record then the previous button is disabled*)
if i_currentuser = i_noofusers-1 then Btnnext.Enabled := False
else Btnnext.Enabled := True; (* if the record we are currently looking at
is the last one then the next button is disabled otherwise it is enabled*)
if i_noofusers = 1 then BtnDelete.Enabled := False
else BtnDelete.Enabled := True; (* if the is only 1 user then the btn
delete is disabled otherwise it is enabled *)
UpdatePanel; //does update the panel
end;

Procedure TFrmUsers.updatepanel; //code for the update panel
begin
Pnlstatus.Caption := 'Record '+
inttostr(i_currentuser + 1) +
' Of ' +
inttostr(i_noofusers);
end;

end.

Comments

  • : Hey ill just paste here my code
    : The issue with it is that it saves random parts of the file it
    : doesnt save passwor and username seperately help plz

    Did you find the issue yet? I don't have Delphi on this machine. What does the text file look like? Is it in the loading or saving that you lose the username/password split?

    Just saw that this went unanswered, so let's see what we can do.
Sign In or Register to comment.

Howdy, Stranger!

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

Categories

In this Discussion