| | |
Delphi Remembering Help Question.
![]() |
•
•
Join Date: Dec 2007
Posts: 21
Reputation:
Solved Threads: 0
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
.
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
. 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:
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:
Some variation of that should work for you.
You can "remember" things between invocations a number of different ways. On Windows, the two most common ways are:
- By using the registry
- 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:
- 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.
- In the FormDestroy procedure, rewrite the file. Write the current price per liter to the file.
Some variation of that should work for you.
•
•
Join Date: Dec 2007
Posts: 21
Reputation:
Solved Threads: 0
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.
. 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)
procedure TfrmCashier.btnSubmitSpeedOfDispenceClick(Sender: TObject); begin SpeedOfDispence:=1; end; procedure TfrmCashier.FormCreate(Sender: TObject); var NewPriceRec : TPrices; filePricerec : TPrices; begin assignfile(Pricesfile,'Staff.dat'); rewrite(Pricesfile); with NewPriceRec do begin PricePerLitre:=PriceLitreEdt.Text; closefile(Pricesfile); end; end; procedure TfrmCashier.FormDestroy(Sender: TObject); begin assignfile(Pricesfile,'Staff.dat'); rewrite(Pricesfile); write(Pricesfile) end; End.
Last edited by Lynxus; Jan 31st, 2008 at 5:53 pm.
Your prices file needs to be the right type.
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.
Pascal Syntax (Toggle Plain Text)
procedure TfrmCashier.FormCreate(Sender: TObject); var NewPriceRec : TPrices; Pricesfile : file of TPrices; // 1 begin assignfile(Pricesfile,'Staff.dat'); try PricePerLitre := 5.999; // 2 reset(Pricesfile); //3 try read( Pricesfile, NewPriceRec ); PricePerLitre := NewPriceRec.PricePerLitre finally closefile(Pricesfile) end except end end;
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.
•
•
Join Date: Dec 2007
Posts: 21
Reputation:
Solved Threads: 0
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
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
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.
You'll have to read up on file handling in Delphi. Here's a very good article I googled.
Good luck.
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.
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.
![]() |
Other Threads in the Pascal and Delphi Forum
- Previous Thread: word list
- Next Thread: External procedure in delphi!
| Thread Tools | Search this Thread |






