Recent

Author Topic: This one should be simple... Opposite of OnCreate event?  (Read 555 times)

OC DelGuy

  • Full Member
  • ***
  • Posts: 114
This one should be simple... Opposite of OnCreate event?
« on: February 05, 2023, 06:56:22 am »
So, I started the application with reading the file from the OnCreate event.  So, when you first start the program, it reads the data file and stores all the values in the records.  Now, which event should I use to write all the data back into the file after I've done some stuff to it (you know, changed a few things and stuff).

OnClose?      OnDestroy?      OnDeactivate?

I'm thinking the opposite would be OnDestroy.  But is that the proper event to use?  I don't plan to have a Tbutton to exit the application.  Just clicking on the close window (the X up in the top right corner with the Minimize and Maximize buttons) is fine by me.

Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

speter

  • Sr. Member
  • ****
  • Posts: 302
Re: This one should be simple... Opposite of OnCreate event?
« Reply #1 on: February 05, 2023, 07:15:02 am »
I tend to use OnClose; particularly if some of the data is coming from other forms/units. I had trouble once using OnDestory because the information was on a secondary form (which had already been destroyed...). If all the data is accessible, it may not matter. I don't think OnDeactivate would be good, because the app may(?) be deactivated (minimised) then later reactivated (restored).

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

OC DelGuy

  • Full Member
  • ***
  • Posts: 114
Re: This one should be simple... Opposite of OnCreate event?
« Reply #2 on: February 05, 2023, 07:17:09 am »
So, if I only have the one form, then OnClose and OnDestroy would be acceptable, right?
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

TRon

  • Hero Member
  • *****
  • Posts: 834
Re: This one should be simple... Opposite of OnCreate event?
« Reply #3 on: February 05, 2023, 07:41:22 am »
@OC DelGuy:
If you have problems deciding where (which event) to put your code then it can help you to not put the actual code in a dedicated event  ;)

What I meant with that is it is better to add two private (or public if you need to access it from another form) methods which do the actual work. One method for loading your data and a second method for storing your data.

If you program like that then you can always switch by making a simple call to these methods from whatever event you decide would be more appropriate (and remove the call at the 'wrong' event).

fwiw: it is never a good idea to load data in an oncreate event (especially your main form) because there is not anything visible yet so in case of failure you have hardly any means to inform the user (but also yourself the developer) about it.

But to answer or question: If you are happy with using those two events (as many others are as well) then yes, it would be acceptable. I use those two all the time when starting a project (eventually I'll remove the code/calls from there and place it/them at a more appropriate location).
« Last Edit: February 05, 2023, 07:50:08 am by TRon »

OC DelGuy

  • Full Member
  • ***
  • Posts: 114
Re: This one should be simple... Opposite of OnCreate event?
« Reply #4 on: February 05, 2023, 08:14:59 am »
@OC DelGuy:
If you have problems deciding where (which event) to put your code then it can help you to not put the actual code in a dedicated event  ;

What I meant with that is it is better to add two private (or public if you need to access it from another form) methods which do the actual work. One method for loading your data and a second method for storing your data.

If you program like that then you can always switch by making a simple call to these methods from whatever event you decide would be more appropriate (and remove the call at the 'wrong' event).
Oh yes, I do get that.  I posted my code in the topic entitled: "Wrong number of Parameters?"  Inside the OnCreate, is a procedure called: "GetVillageData".  That's the one that loads the data from file.  It's just in the OnCreate so it runs first.  I think that's what you mean...



fwiw: it is never a good idea to load data in an oncreate event (especially your main form) because there is not anything visible yet so in case of failure you have hardly any means to inform the user (but also yourself the developer) about it.

But to answer or question: If you are happy with using those two events (as many others are as well) then yes, it would be acceptable. I use those two all the time when starting a project (eventually I'll remove the code/calls from there and place it/them at a more appropriate location).
So, if not on the OnCreate, then where to ensure it happens in the beginning, before the user starts doing anything?
And while on the subject, which event would be best for writing the data back to the file at the end (when the user closes the program)?
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

circular

  • Hero Member
  • *****
  • Posts: 3961
    • Personal webpage
Re: This one should be simple... Opposite of OnCreate event?
« Reply #5 on: February 05, 2023, 08:31:36 am »
In fact, it may be better to put the code in the OnCloseQuery event. This way you can ask for confirmation "Do you want to save changes" with Yes/No/Cancel buttons and if Cancel button is pressed, you can cancel the closing of the form.
Conscience is the debugger of the mind

TRon

  • Hero Member
  • *****
  • Posts: 834
Re: This one should be simple... Opposite of OnCreate event?
« Reply #6 on: February 06, 2023, 04:28:49 am »
Oh yes, I do get that.  I posted my code in the topic entitled: "Wrong number of Parameters?"  Inside the OnCreate, is a procedure called: "GetVillageData".  That's the one that loads the data from file.  It's just in the OnCreate so it runs first.  I think that's what you mean...
Close but no cigar  :)

I meant making a separate method for it. It is the same as a procedure but that procedure being part of the form (I know it perhaps sounds strange but when a procedure is part of a object/class it is named a method).

Code: Pascal  [Select][+][-]
  1.   TForm1 = class(TForm)
  2.     Farmers             : TEdit;
  3.     Artisans            : TEdit;
  4.     Laborers            : TEdit;
  5.     ...
  6.     procedure FormCreate(Sender: TObject);    
  7.   private
  8.     procedure GetVillageData;  // in IDE when you added this declaration you can press ctrl-shift-c for auto completion
  9.   public
  10.   end;
  11.  
  12.  
  13. procedure TForm1.GetVillageData;
  14. Var
  15.   FilenameIn : Textfile;
  16.   Recds, y : Integer;
  17. Begin
  18.   {Proc GetVillageData}
  19.   Assignfile(FilenameIn, 'VillageData.txt');
  20.   Recds := 1;
  21.   Try
  22.     Reset(FilenameIn);
  23.     While not EOF(FilenameIn) do
  24.     Begin {While}
  25.       ReadLn(FilenameIn, Villages[Recds].Name);
  26.       ReadLn(FilenameIn, Villages[Recds].Inn);
  27.       ReadLn(FilenameIn, Villages[Recds].Government);
  28.       ReadLn(FilenameIn, Villages[Recds].Population);
  29.       ReadLn(FilenameIn, Villages[Recds].Families);
  30.       ReadLn(FilenameIn, Villages[Recds].Farmers);
  31.       ReadLn(FilenameIn, Villages[Recds].Artisans);
  32.       ReadLn(FilenameIn, Villages[Recds].Laborers);
  33.       ReadLn(FilenameIn, Villages[Recds].Paupers);
  34.       For y := 1 to 3 do
  35.       Begin {For y := 1 to 3 do}
  36.         ReadLn(FilenameIn, Villages[Recds].Gods[y]);
  37.         ReadLn(FilenameIn, Villages[Recds].Taverns[y]);
  38.         ReadLn(FilenameIn, Villages[Recds].Trade[y]);
  39.       end; {For y := 1 to 3 do}
  40.       Recds := Recds + 1;
  41.     end; {While}
  42.  
  43.     Close(FilenameIn);
  44.   finally
  45.   end; {Try Finally}
  46.  end;
  47.  
  48.  
  49. procedure TForm1.FormCreate(Sender: TObject);
  50. begin
  51.   GetVillageData;
  52.   // invoke another 100 initialization routines.
  53. end;  
  54.  


So, if not on the OnCreate, then where to ensure it happens in the beginning, before the user starts doing anything?
That depends on the situation. I use custom event messages for that (but for simple programs/tools that might perhaps be overkill). Some people even use timers for that (which imho is wrong).

You could for example use TApplication.QueueAsyncCall or Application.OnIdle (the latter only once ofc.)

I usually load data after the main form has been displayed (unless the program has a so called splash screen that informs the user about loading/startup progress). The mentioned events can help you accomplish that. But you can use other as well (formshow for example, again only once. but in that case you better have a look at AddHandlerFirstShow )

Quote
And while on the subject, which event would be best for writing the data back to the file at the end (when the user closes the program)?
Under normal circumstance a user would load and save data manually. So any button/menu press can be used for such cases. Using an event such as suggested by user circular is a pretty good one as well.
« Last Edit: February 06, 2023, 04:33:46 am by TRon »

 

TinyPortal © 2005-2018