Delphi Remembering Help Question.

Reply

Join Date: Dec 2007
Posts: 21
Reputation: Lynxus is an unknown quantity at this point 
Solved Threads: 0
Lynxus Lynxus is offline Offline
Newbie Poster

Delphi Remembering Help Question.

 
0
  #1
Jan 31st, 2008
I have a petrol pump simulator system and the Price Per Litre is entered through an Edit box and then the Images for each digit is displayed in an Imagebox. That part works fine, but however eachtime the application is closed the Price per litre is reset and It has to be re-entered, I would like it to be rememberd so that when I open Delphi again it will already be set.

I understand the rules and don't expect all the code to just be given to me I just need help on understanding how to go about this.

Thanks, all help appreciated .
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Delphi Remembering Help Question.

 
0
  #2
Jan 31st, 2008
Everytime an application is terminated, all the variable's values are lost.

You can "remember" things between invocations a number of different ways. On Windows, the two most common ways are:
  1. By using the registry
  2. By using a file

Number 2 would be simpler for you right now (but if you want to do #1 I can give you a useful example, just ask). To do #2, just take the following steps:
  1. In the FormCreate procedure, try to open the file. If you can, try to read the saved price per litre. If you can't do either, just set the price per litre to some default or ask the user for an initial value.
  2. In the FormDestroy procedure, rewrite the file. Write the current price per liter to the file.

Some variation of that should work for you.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 21
Reputation: Lynxus is an unknown quantity at this point 
Solved Threads: 0
Lynxus Lynxus is offline Offline
Newbie Poster

Re: Delphi Remembering Help Question.

 
0
  #3
Jan 31st, 2008
Ok. Thanks for your quick reply, I didn't expect one so quick so wasn't checking . Here is some code I have come up with although it doesn't quite work because I don't know much about file handling.

Pascal and Delphi Syntax (Toggle Plain Text)
  1. procedure TfrmCashier.btnSubmitSpeedOfDispenceClick(Sender: TObject);
  2. begin
  3. SpeedOfDispence:=1;
  4. end;
  5.  
  6. procedure TfrmCashier.FormCreate(Sender: TObject);
  7. var
  8. NewPriceRec : TPrices;
  9. filePricerec : TPrices;
  10. begin
  11. assignfile(Pricesfile,'Staff.dat');
  12. rewrite(Pricesfile);
  13. with NewPriceRec do
  14. begin
  15. PricePerLitre:=PriceLitreEdt.Text;
  16. closefile(Pricesfile);
  17. end;
  18. end;
  19.  
  20. procedure TfrmCashier.FormDestroy(Sender: TObject);
  21. begin
  22. assignfile(Pricesfile,'Staff.dat');
  23. rewrite(Pricesfile);
  24. write(Pricesfile)
  25. end;
  26.  
  27. End.
Last edited by Lynxus; Jan 31st, 2008 at 5:53 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Delphi Remembering Help Question.

 
0
  #4
Jan 31st, 2008
Your prices file needs to be the right type.
  1. procedure TfrmCashier.FormCreate(Sender: TObject);
  2. var
  3. NewPriceRec : TPrices;
  4. Pricesfile : file of TPrices; // 1
  5. begin
  6. assignfile(Pricesfile,'Staff.dat');
  7. try
  8. PricePerLitre := 5.999; // 2
  9. reset(Pricesfile); //3
  10. try
  11. read( Pricesfile, NewPriceRec );
  12. PricePerLitre := NewPriceRec.PricePerLitre
  13. finally
  14. closefile(Pricesfile)
  15. end
  16. except end
  17. end;
1. The prices file is a file of TPrices records.
2. The default price per litre. (5.999. I don't know what type of thing PricePerLitre, which I assume is a class variable, is, so I assume it is a double.)
3. To read the file, use reset. To overwrite the file, use rewrite.

The try blocks are there to make sure things work smoothly. You might want to add something in the except block (for example, ask the user what the default price per litre is).

The FormDestroy should look similar, except you should write the file instead of read it.

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 21
Reputation: Lynxus is an unknown quantity at this point 
Solved Threads: 0
Lynxus Lynxus is offline Offline
Newbie Poster

Re: Delphi Remembering Help Question.

 
0
  #5
Jan 31st, 2008
Okay well I entered in the code and PricePerLitre I haven't declared atall. What do I declare it as because whatever I put it doesn't seem to work,
Also what do I put in the form destroy procedure because I'm not sure what I posted up there is completely right?

from there I think I can do the rest.

Thanks very much
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Delphi Remembering Help Question.

 
0
  #6
Jan 31st, 2008
Question: What is the type of TPrices? Is it a record?

You'll have to read up on file handling in Delphi. Here's a very good article I googled.

Good luck.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 21
Reputation: Lynxus is an unknown quantity at this point 
Solved Threads: 0
Lynxus Lynxus is offline Offline
Newbie Poster

Re: Delphi Remembering Help Question.

 
0
  #7
Jan 31st, 2008
TPrices is a Record.

And at school we've been through that same guide but it is useful, it's just a case of reminding myself I think.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Delphi Remembering Help Question.

 
0
  #8
Jan 31st, 2008
Ah, OK. So to write to the file you'll need to open it, much like I did above, but using the rewrite procedure, and write a TPrices record to the file of TPrices. Once done, close the file. The format of your FormDestroy procedure should look a lot like the FormCreate procedure.

I assumed that PricePerLitre was a global or a class variable because of the way you used it in the code you posted.

Hope this helps.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Pascal and Delphi Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC