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 :).

Recommended Answers

All 7 Replies

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.

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

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.

Your prices file needs to be the right type.

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;

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.

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 :)

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.