execute codes when form1 is ready lazarus / 21-General ---------------------------------------------------------------------- Ericktux · 2024-01-29 03:07 Hello friends, good day, a question, I only have "form1" and I need to put codes when form1 finishes its construction and is ready. because in the create event it is executed quickly without having finished building form1, which generates an error. Where would it be best to put startup codes? ---------------------------------------------------------------------- TRon · 2024-01-29 03:54 Quote from: Ericktux on January 29, 2024, 04:07:17 am...I need to put codes when form1 finishes its construction and is ready. because in the create event it is executed quickly without having finished building form1, which generates an error. Those two sentences are contradicting with each other. The form's oncreate event is invoked after the construction of the form so that is an indication that somewhere else in your code is something happening. In order to solve your issue like you suggest yourself you would first have to figure out what 'construction' is still happening  when you execute the code that throws you an error, then wait for that 'contruction' to finish and after that is done call your (current error generating) code. I would suspect that you have several components on your form that are intertwined with each other to such an extend that in your oncreate event you set a property or execute a method that trigger an avalanche of actions so that you are unable to see for yourself what is actually happening and/or which code is executed. But if you believe that doe snot apply for your situation then you can quickly check that by placing your (currently error generating) code at the application properties onidle event. Just make sure that the code in the event (or the event itself) is only executed once. ---------------------------------------------------------------------- cdbc · 2024-01-29 04:56 Hi How about "QueueAsyncCall" at the end of "FormCreate"? Regards Benny ---------------------------------------------------------------------- Thaddy · 2024-01-29 06:26 Override TForm.AfterConstruction method. Don't forget to call inherited AfterConstruction first, then add the code you need. AfterConstruction is called automatically, do not call it manually. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs; type   { TForm1 }   TForm1 = class(TForm)  private   public     procedure AfterConstruction;override;  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.AfterConstruction;begin  inherited AfterConstruction;  Color := clRed;  Caption := DateTimeToStr(Now);end; end.Dead easy. And how it should be done. ---------------------------------------------------------------------- rvk · 2024-01-29 06:50 Quote from: Ericktux on January 29, 2024, 04:07:17 amHello friends, good day, a question, I only have "form1" and I need to put codes when form1 finishes its construction and is ready. because in the create event it is executed quickly without having finished building form1, which generates an error. Where would it be best to put startup codes?  Do you really mean after construction or do you mean after showing on screen? Those are different things (for example you can't set focus for certain things before they are visible). What error message do you get exactly for what line of code? ---------------------------------------------------------------------- Thaddy · 2024-01-29 07:21 Although it is possible to modify the OnShow event in afterconstruction, and e.g. database code can be set up if needed and is available... Not even necessary, since you can manipulate the taborder you can manipulate focus. Add two edits to my example:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.AfterConstruction;begin  inherited AfterConstruction;  Color := clRed;  Caption := DateTimeToStr(Now);  edit2.TabOrder :=0;end; AfterConstruction really is the place to handle any additional set-ups in your code. It guarantees that everything on the form is available and you can work with it. ---------------------------------------------------------------------- rvk · 2024-01-29 08:41 Quote from: Thaddy on January 29, 2024, 08:21:23 amAlthough it is possible to modify the OnShow event in afterconstruction, and e.g. database code can be set up if needed and is available... Yes. Most things can be set up in AfterConstruction. Actually, AfterConstruction is the one that calls DoCreate so you could even do most of those things in FormCreate (The OnCreate event). There is no need to override AfterConstruction for that. Just double click the OnCreate for the form and do it in the FormCreate event. But there are some things which can only be done when the form is visible. Yes, the one time SetFocus could be done by taborder but there are, more complex visible things (like executing actions and updating certain visible things), which can't be done before Show (so in AfterConstruction or OnCreate). But that's very very very rare and most of the time you can get around it with a few tweaks. But that's why I asked what error message TS got at what line of code. If that was given directly we could have just given a more specific answer. ---------------------------------------------------------------------- Thaddy · 2024-01-29 09:16 I agree we can give a more precise answer of OP writes what he really wants to achieve. (That is with the proverbial "as always".) ---------------------------------------------------------------------- Zvoni · 2024-01-29 09:27 If i get into the situation, that i really, really, have to wait for a Form to finish showing up (incl. creation etc.), i usuall define a Public Procedure in the Form Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TForm1 = class(TForm)  private   public    Procedure DoSomething;  end;               And in the lpr-file Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i begin  RequireDerivedFormResource:=True;  Application.Scaled:=True;  Application.Initialize;  Application.CreateForm(TForm1, Form1);  Form1.DoSomething;  Application.Run;end.             NotaBene: Only non-blocking-Code should be used in that procedure, so no ShowMessage or whatever ---------------------------------------------------------------------- Thaddy · 2024-01-29 09:29 That is bad programming. ---------------------------------------------------------------------- Ericktux · 2024-01-29 10:12 Quote from: rvk on January 29, 2024, 07:50:04 amQuote from: Ericktux on January 29, 2024, 04:07:17 amHello friends, good day, a question, I only have "form1" and I need to put codes when form1 finishes its construction and is ready. because in the create event it is executed quickly without having finished building form1, which generates an error. Where would it be best to put startup codes?  Do you really mean after construction or do you mean after showing on screen? Those are different things (for example you can't set focus for certain things before they are visible). What error message do you get exactly for what line of code? Hello friend, exactly what I mean is after displaying on the screen. ---------------------------------------------------------------------- Ericktux · 2024-01-29 10:31 I have several components in my form and codes that are intertwined with each other. and for them to work well, form1 must be shown first. I have noticed that in the oncreate event my codes execute quickly without displaying form1 which generates errors ---------------------------------------------------------------------- KodeZwerg · 2024-01-29 10:44 Quote from: Ericktux on January 29, 2024, 11:31:46 amI have several components in my form and codes that are intertwined with each other. and for them to work well, form1 must be shown first. I have noticed that in the oncreate event my codes execute quickly without displaying form1 which generates errors  Since you still not answered the important questions we can just assume or guess. So my guess is that you have a bad code design that rely on visuals, split your UI from logic and all is good. ---------------------------------------------------------------------- rvk · 2024-01-29 10:51 Quote from: Ericktux on January 29, 2024, 11:31:46 amI have noticed that in the oncreate event my codes execute quickly without displaying form1 which generates errors  We can't help you if you are not more specific. What error messages and what lines? Show us the code from your OnCreate (that will explain a lot). (Most things you can do with a component without being visible but there might be exceptions.) ---------------------------------------------------------------------- Thaddy · 2024-01-29 11:51 those are very rare exceptions. First try my code. Which does not even need the form to be shown, It is called just before the form is shown the first time and guatantees that the form and all its children are ready to use just before the form is show. Therefor it also does not fire any paint or other events prematurely firing, so the code is faster too. Otherwise we need indeed some more info, preferebly example code. The OnCreate event is usually less suitable. (Although many code examplies reliy on that, that is usually not the proper way) But you can also use the OnSHow or OnActivate event with a flag to ensure the event is called only once. But still do what Rik and I asked and be more precise in what you want to achieve. Or do you not understand my example? (yet)? Because that is textbook. ---------------------------------------------------------------------- TRon · 2024-01-29 12:34 Quote from: Ericktux on January 29, 2024, 11:31:46 amI have several components in my form and codes that are intertwined with each other. and for them to work well, form1 must be shown first. I have noticed that in the oncreate event my codes execute quickly without displaying form1 which generates errors  In that case just telling us about it is not going to be very helpful. As others pointed out some code that is responsible for producing the error (and what error exactly) is required. Because the coupling between the different components it might not be enough to only show your code in the oncreate event. I am not sure if i understood the gist of what you wrote (to others as well) but in case your code performs actions in the onshow event and execute your error generating code in the oncreate event then that is more or less to be expected. If the latter is indeed the case then you really would need to figure out what the last action is for the form to show (or what method call or property change triggered the last updates to your (other) component(s) and invoke your code after that method call/property change. How to do that can be approach in different ways but the simplest is just stuffing your code into a custom defined method and call that method at the right moment. Usually you can figure out the order of events by using the debugger (but can't remember if you had issues using the debugger because of the host-dev platform being used). Alternatively use writelns and start your application from console/terminal (though Lazarus does have a output terminal/window tab). ---------------------------------------------------------------------- Ericktux · 2024-01-29 13:01 Hello friends, for example, when I put all my codes in oncreate I get an error... but If I wait for form1 to finish being built and it is shown with focus there, my code works. I would simply like to know if there is an "event" that is fired only once when form1 finishes displaying. It would simplify me a lot, it is more practical for me Do you think that any of these events will help me with that: *OnCreate *OnShow *OnPaint *OnActivate *OnResize ?? ---------------------------------------------------------------------- Zvoni · 2024-01-29 13:05 Quote from: Ericktux on January 29, 2024, 02:01:29 pmHello friends, for example, when I put all my codes in oncreate I get an error... but If I wait for form1 to finish being built and it is shown with focus there, my code works. I would simply like to know if there is an "event" that is fired only once when form1 finishes displaying. It would simplify me a lot, it is more practical for me Do you think that any of these events will help me with that: *OnCreate *OnShow *OnPaint *OnActivate *OnResize ?? ....and Thaddy already answered that Question, or what do you think how often "AfterConstruction" fires? Besides that: My Approach will work (irrespective it has been called "bad programming"), if you put the code, that generates that still undefined error, into that Procedure. And as i said: That code must not contain any blocking (ShowMessage, or anything waiting for User-Input) ---------------------------------------------------------------------- Thaddy · 2024-01-29 13:24 Ok, I withdraw any remarks about programming style,it was not meant to offend, but what you have to realize is that afterconstruction is only called after any and all dependencies are also resolved. Focussing on the use of events does not likely be the good solution. (I am being mild here.. ) ---------------------------------------------------------------------- dseligo · 2024-01-29 13:30 Quote from: Ericktux on January 29, 2024, 02:01:29 pmHello friends, for example, when I put all my codes in oncreate I get an error... You still didn't specify what is exact error you get. You can also attach screenshot of error. ---------------------------------------------------------------------- Zvoni · 2024-01-29 13:32 Quote from: Thaddy on January 29, 2024, 02:24:09 pmOk, I withdraw any remarks about programming style,it was not meant to offend, but what you have to realize is that afterconstruction is only called after any and all dependencies are also resolved. Focussing on the use of events does not likely be the good solution. Thaddy, i think the "confusion" stems from the fact, that there is no documentation about all the events of a TForm and the Order in which they are fired. At least  i've not found any comprehensive documentation for it, except the official docs where the Events themselves are described, but nowhere the ORDER (Which one fires first, which one fires last etc.) And no, i wasn't offended by your remark, since it might offer insight to me, and i can learn something from it. But even you have to admit (irrespective of "bad programming") that my approach WILL WORK with the rules i stipulated ---------------------------------------------------------------------- Ericktux · 2024-01-29 13:33 Quote from: Thaddy on January 29, 2024, 02:24:09 pmOk, I withdraw any remarks about programming style,it was not meant to offend, but what you have to realize is that afterconstruction is only called after any and all dependencies are also resolved. Focussing on the use of events does not likely be the good solution. Don't worry brother, everything is fine, they have given me a good idea. "But you can also use the OnSHow or OnActivate event with a flag to ensure the event is called only once."  I find it more practical and simple ---------------------------------------------------------------------- Thaddy · 2024-01-29 13:37 Yes, but what I meant is that it slows down the code. Depending on the context quite considerably. My point is, and I maintain that, is that you do not need events in most if not all cases. Events should be your last resource regarding form construction. That is why a simple overridden AfterConstruction can help. But if you and Rik and I do not see any code, we can hardly help him. ---------------------------------------------------------------------- rvk · 2024-01-29 13:38 Quote from: Zvoni on January 29, 2024, 02:32:52 pmAnd no, i wasn't offended by your remark, since it might offer insight to me, and i can learn something from it. But even you have to admit (irrespective of "bad programming") that my approach WILL WORK with the rules i stipulated It will definitely work but normally you don't call Form1 procedure like that in the main source code For me, ALL forms are created dynamically (because most forms can be opened multiple times at the same time and a Form1 won't cut it in that case). I've used the method of PostMessage at the end of FormCreate() in the past, to let a Form procedure fire when all queue handling is done. That way, it will work for all sorts of TForms, including those that are created dynamically. ---------------------------------------------------------------------- TRon · 2024-01-29 13:49 Quote from: Ericktux on January 29, 2024, 02:01:29 pmDo you think that any of these events will help me with that: *OnCreate *OnShow *OnPaint *OnActivate *OnResize I am going to withdraw from the discussion after this message as there is too much noise which works confusing for TS but I will write the following. Using an event is not the solution (unless it is your own custom one) But I did state to use tapplicationproperties.onidle for testing purpose besides debugging your code to determine the location of your problem (or solution). What you should focus on, is to determine when the "creation code" is ready, then set a state (or fire/invoke a custom event) and run your current offending code (which retires when the state is not applicable). There is not one single magic event that is suitable for all situations simply because it depends on what event you have already implemented and/or connected to other components that is possible to trigger other actions on yet other components and what code you are actually executing and what actions your code performs. ---------------------------------------------------------------------- Zvoni · 2024-01-29 13:51 Quote from: rvk on January 29, 2024, 02:38:09 pmIt will definitely work but normally you don't call Form1 procedure like that in the main source code For me, ALL forms are created dynamically (because most forms can be opened multiple times at the same time and a Form1 won't cut it in that case). I've used the method of PostMessage at the end of FormCreate() in the past, to let a Form procedure fire when all queue handling is done. That way, it will work for all sorts of TForms, including those that are created dynamically. You mean later in the code? in the form of Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Var  frmSomeForm:TMyForm; //Somewhere over the Rainbow....frmSomeForm:=TMyForm.Create;  //OnCreate and other Events are firingfrmSomeForm.MySpecialProcedure;//blablabla //Somewhere elsefrmSomeForm.Free;  My Approach would still work in that case. Yes, i'm aware of the "downsides" to it: Remembering that i have to call the Procedure explicitely, but as always in life: Many ways to skin a cat... ---------------------------------------------------------------------- TRon · 2024-01-29 14:11 Quote from: Zvoni on January 29, 2024, 02:32:52 pmAt least  i've not found any comprehensive documentation for it, except the official docs where the Events themselves are described, but nowhere the ORDER (Which one fires first, which one fires last etc.) Best I can do is this but a wiki is hardly official documentation. But also that actually does not matter much. Do you have any notion if TS is perhaps using an event to a component that when certain action happens (property set or otherwise a show or activate event) it will try to invoke something on form2 but that form was not created yet, producing an access violation ? no code, no answer. and there are many of such situations. ---------------------------------------------------------------------- Thaddy · 2024-01-29 14:14 Best I can do is simply: https://www.freepascal.org/docs- html/rtl/system/tobject.afterconstruction.html Fairly obvious isn't it? ---------------------------------------------------------------------- Zvoni · 2024-01-29 14:40 Quote from: Thaddy on January 29, 2024, 03:14:40 pmBest I can do is simply: https://www.freepascal.org/docs- html/rtl/system/tobject.afterconstruction.html Fairly obvious isn't it? Well, yes, but it also states Quoteto provide specific behaviour that is executed after the constructor has finished executing."Constructor" No mention of OnShow, OnActivate, OnPaint etc... ---------------------------------------------------------------------- Zvoni · 2024-01-29 14:42 Quote from: TRon on January 29, 2024, 03:11:28 pmBut also that actually does not matter much. Do you have any notion if TS is perhaps using an event to a component that when certain action happens (property set or otherwise a show or activate event) it will try to invoke something on form2 but that form was not created yet, producing an access violation ? no code, no answer. and there are many of such situations. From Post #1 Quote I only have "form1" and I need to put codes when form1 finishes its construction and is ready.That said: It's basically the same what i wrote: is there blocking code executed? ---------------------------------------------------------------------- TRon · 2024-01-29 14:51 Quote from: Zvoni on January 29, 2024, 03:42:05 pmFrom Post #1 Ok, you got me there  I was using a second form as a more generic example. I do not have the wisdom of knowing all components in the world inside out and know what events they trigger (and user has assigned) when creating/showing/activating/whatever event them). QuoteThat said: It's basically the same what i wrote: is there blocking code executed? To that we can indeed agree. That is why I personally do the same as rvk and assign/create everything dynamically so that I know what I'm doing, and besides that allows me to setup a form whenever it is necessary (and not sooner). RAD can also mean rapid application disaster ---------------------------------------------------------------------- Thaddy · 2024-01-30 11:52 Why does OP focus on events? All of us -1 can solve his problem in seconds, not minutes. I have NOT seen a response WHY he wants to use events..... Show us what you want to achieve and describe it better.