Recent

Author Topic: FormCreate(Sender : TObject) firing several times?  (Read 7089 times)

PaulANormanNZ

  • Full Member
  • ***
  • Posts: 115
FormCreate(Sender : TObject) firing several times?
« on: May 25, 2018, 06:50:26 am »
Hi,

In general, what could be causing a message dialog say...

procedure TForm1.FormCreate(Sender: TObject);
begin

showmessage(LazFileUtils.ExtractFileNameOnly(application.ExeName));

// etc blah blah

end;

To show several times on startup please?

Paul

tr_escape

  • Sr. Member
  • ****
  • Posts: 432
  • sector name toys | respect to spectre
    • Github:
Re: FormCreate(Sender : TObject) firing several times?
« Reply #1 on: May 25, 2018, 08:23:05 am »
Maybe you can use onShow event.

or

You can create a timer (tmrStartUp) and your timer has this code (like as):

Code: Pascal  [Select][+][-]
  1. procedure TfrmMain.FormShow(Sender: TObject);
  2. begin
  3.    tmrStartUp.Enabled:=True;  //start your timer
  4. end;
  5.  
  6.  
  7. procedure TfrmMain.tmrStartUpTimer(Sender: TObject);
  8. begin
  9.   tmrStartUp.Enabled:=False;
  10.  
  11.   try
  12.      DoSomeThingAtFormStartUp;
  13.   except
  14.     tmrStartUp.Enabled:=True;
  15.     tmrStartUp.Interval:=tmrStartUp.Interval*2;   // I don't know why I am added try little bit later maybe it is up to your usage
  16.   end;  
  17.  
  18. end;
  19.  
  20.  

PaulANormanNZ

  • Full Member
  • ***
  • Posts: 115
Re: FormCreate(Sender : TObject) firing several times?
« Reply #2 on: May 25, 2018, 12:38:35 pm »
Thanks tr_escape,

I mean

Quote
To show several times on startup please?

Its a problem that is already happening, seemingly for no apparent reason.

What sort of thing should I be looking for as a possible cause please?

Paul

Lazarus 1.8.0 r56594 FPC 3.0.4 x86_64-win64-win32/win64

tr_escape

  • Sr. Member
  • ****
  • Posts: 432
  • sector name toys | respect to spectre
    • Github:
Re: FormCreate(Sender : TObject) firing several times?
« Reply #3 on: May 25, 2018, 12:58:50 pm »
As I understand is you want to only few times this message? For example you have a software but you want to show an info page/message for only 5 times, after that your software will not show this message or page.

If so, you have to keep your showing times of your form/page or message in the disk as in ini file or in registry.


Thats can help you:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
  9.  
  10.   LazFileUtils,IniFiles;
  11.  
  12. type
  13.  
  14.   { TForm1 }
  15.  
  16.   TForm1 = class(TForm)
  17.     procedure FormCreate(Sender: TObject);
  18.   private
  19.  
  20.   public
  21.  
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.   ini : TIniFile;
  27.   dir : String;
  28.   count : Integer=0;
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. { TForm1 }
  34.  
  35. procedure TForm1.FormCreate(Sender: TObject);
  36. begin
  37.   dir := GetCurrentDir;
  38.   try
  39.     ini :=TIniFile.Create(dir+DirectorySeparator+'mystorage.ini');
  40.     count:= ini.ReadInteger('SETTINGS','MyCount',0);
  41.     if count<=5 then
  42.       begin
  43.         ShowMessage(LazFileUtils.ExtractFileNameOnly(application.ExeName)+' Cnt:'+IntToStr(count));
  44.         count:=count+1;
  45.         ini.WriteInteger('SETTINGS','MyCount',count);
  46.       end;
  47.   finally
  48.     ini.Free;
  49.   end;
  50. end;
  51.  
  52. end.
  53.  
« Last Edit: May 25, 2018, 01:04:56 pm by tr_escape »

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: FormCreate(Sender : TObject) firing several times?
« Reply #4 on: May 25, 2018, 01:43:50 pm »
Maybe you accidentally assigned FormCreate event elsewhere. I didn't ever see bug where constructor of main form run twice.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

PaulANormanNZ

  • Full Member
  • ***
  • Posts: 115
Re: FormCreate(Sender : TObject) firing several times?
« Reply #5 on: May 26, 2018, 09:14:06 am »
Maybe you accidentally assigned FormCreate event elsewhere. I didn't ever see bug where constructor of main form run twice.

Thanks I'll check - but I'd be surprised! - but who knows!  %)

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: FormCreate(Sender : TObject) firing several times?
« Reply #6 on: May 26, 2018, 09:28:35 am »
A single form fires only once, but the auto-created forms in a project will ALL fire a formcreate. Can that be the case?
Since TCustomForm is the root of them all, it may look like mainform fires them, but these are simply the other forms in a project that cause this behavior.
You can check that by  logging the classname in the event. These are different for all if not most forms. This all happens before Application.Run.

If it really annoys you, simply do not auto-create the forms, but make them create on demand.
« Last Edit: May 26, 2018, 09:30:55 am by Thaddy »
Specialize a type, not a var.

guest62577

  • Guest
RE: OnCreate Event Occurring Several Times?
« Reply #7 on: May 27, 2018, 09:28:13 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3. //Autofix Standards to ensure RunOnce Situations;
  4. Form1.OnCreate:=Nil;
  5.  
  6. showmessage(LazFileUtils.ExtractFileNameOnly(application.ExeName));
  7.  
  8. // etc blah blah
  9.  
  10. end;

Josh

  • Hero Member
  • *****
  • Posts: 1271
Re: FormCreate(Sender : TObject) firing several times?
« Reply #8 on: May 27, 2018, 09:54:45 pm »
Hi

Be careful what you use in the form.create, as this is executed before the form is ready to show.

I would only use form.create to set any globals etc. very minimal stuff, so that the form can be shown,.

I would use form.activate to do any checks that require a visual response.

You still have to be careful with form.activate as this can get fired multiple times,  so the simple solution is use a global to mark that you have run the activate routine ie
Code: Pascal  [Select][+][-]
  1. uses .......;
  2.  
  3. type ........;
  4.  
  5. var FActivated:boolean=false;
  6.  
  7.  
  8.  
  9. .....
  10.  
  11. procedure form1.FormCreate(Sender: TObject);
  12. begin
  13.   // Setup any required before the form is shown.
  14.   // Do not overdo this section; anything that takes time to do; place in onactivate
  15.   ......
  16. End;
  17.  
  18.  
  19. procedure form1.FormActivate(Sender: TObject);
  20. begin
  21.   if FActivated=false then
  22.   begin
  23.      FActivated:=true; // immediately set to true so that the following code only gets run Once.
  24.      // Following code will only run once. Also the form should be come visible whilst this routine is running,
  25.      // so if itakes some time the user will be aware that the app is working.
  26.      .....
  27.     ......
  28.   end;
  29. end;
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: FormCreate(Sender : TObject) firing several times?
« Reply #9 on: May 27, 2018, 11:38:05 pm »
a bad practice which I have done myself is to create a form with all the needed controls at
design time, and then dynamically create multiples of that form at runtime.

 The problem is that under normal create they'll read the associated  resource and that includes assigning the
events of the design time to your newly created one's..

 Unless things have changed that is how it used to be.

 What I did was to test the SENDER while in the OnCreate event...

Example:

 if Sender = FORM1 then
 begin
  …
 end;


 If you don't want this functionality you could always try  "CreateNew" constructor instead but then you need to
assign some items at runtime..

And then again, I could be off my rocker!
 :D
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018