Stopping multiple calculations lazarus / 21-General ---------------------------------------------------------------------- J-G · 2022-06-04 09:48 I'm using a TEdit with an OnEditingDone event which works as expected but the control is still 'live' so whatever I do next - say, click another control - the calculations for the OnEditingDone Event are repeated. I assume this is because the TEdit still has [Focus] but when I try to set .Focused := false  as : 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.TPI_ValEditingDone(Sender: TObject);begin  CalcGearing;  CalcGear2Pos;  TPI_Val.Focused := false;end; I get an 'Error: Argument cannot be assigned to' It seems that I'm missing something quite simple but 'what' that might be is beyond my wit! ---------------------------------------------------------------------- wp · 2022-06-04 10:26 Quote from: J-G on June 04, 2022, 11:48:45 amclick another control - the calculations for the OnEditingDone Event are repeated.That's the way OnEditingDone works. It fires when the user presses ENTER or focusses another control. Is there really a problem when the calculation is repeated several times? If it is, you must keep track of the validity of the input parameters and the calculation result. ---------------------------------------------------------------------- J-G · 2022-06-04 10:56 Quote from: wp on June 04, 2022, 12:26:14 pmIs there really a problem when the calculation is repeated several times? Not an unsurmountable one WP  -  during developement & debugging I'm using a [Memo] to report the calculation results and when I click on that to review the figures, they are repeated. As a second attempt I've just tried to set the [Memo]  to .focused := true but get the same message so it looks as if I'll have to put up with it  -  unless anyone can suggest a better 'event' ?  Maybe [OnChange]?  - - -  I'll test that. . .    Hmm... yes it gets rid of the repeat BUT it has other 'issues' - such as using a pre-definded value doesn't fire the event but the calculations are done at every 'change' --- ie. if the figure needs to be 20 then the event fires at the 'delete', then '2' and again at '20'  - exactly as you would expect of course. ---------------------------------------------------------------------- wp · 2022-06-04 11:28 As I said you must keep track of the validity of the calculation. It should be re-executed only when the result is not valid. Have a look at the attached demo in which the calculation simply has the task to double the value in an edit control. A simpler way would be to add a button "Calculate" - only when you click on the button, the calculation is executed. ---------------------------------------------------------------------- Warfley · 2022-06-04 11:44 You cant just set focus to false, some control must always have the focus. You must focus a different control if you want your edit to lose focus (i think the method is named SetFocus I.e. Button1.SetFocus switches focus to button1) ---------------------------------------------------------------------- wp · 2022-06-04 11:47 Quote from: Warfley on June 04, 2022, 01:44:17 pmYou cant just set focus to false, some control must always have the focus. You must focus a different control if you want your edit to lose focus (i think the method is named SetFocus I.e. Button1.SetFocus switches focus to button1) Yes, the method is called SetFocus. But it WILL fire OnEditingDone. ---------------------------------------------------------------------- winni · 2022-06-04 12:51 Hi! Easy way to get around double execution: * Define a boolean var in the private section of your 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 ...privateCalculationDone: Boolean;... * In the OnEnter procedure of the Edit set  CalculationDone to false * In your calculation procedure do this: 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 beginif CalculationDone then exit;// Do your calculationCalculationDone := true;end; Now the calculation is only executed once. Winni ---------------------------------------------------------------------- MarkMLl · 2022-06-04 13:07 Easier way: a typed constant in the event handler acting as a static variable, set to Now() at recalculation, with a check on entry to make sure that "the sums" haven't just been done. MarkMLl ---------------------------------------------------------------------- J-G · 2022-06-04 13:29 Quote from: wp on June 04, 2022, 01:28:50 pmAs I said you must keep track of the validity of the calculation. It should be re-executed only when the result is not valid. Have a look at the attached demo in which the calculation simply has the task to double the value in an edit control.That's the way I'm thinking now - thanks. If (as you say) SetFocus will not get round the issue, I'll settle for 'CalcDone' and checking - or something similar Quote from: wpA simpler way would be to add a button "Calculate" - only when you click on the button, the calculation is executed.That's the way I've done it in the past but consider it 'clumsy' ---------------------------------------------------------------------- tetrastes · 2022-06-04 15:06 Quote from: J-G on June 04, 2022, 11:48:45 amI assume this is because the TEdit still has [Focus] but when I try to set .Focused := false  as : 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.TPI_ValEditingDone(Sender: TObject);begin  CalcGearing;  CalcGear2Pos;  TPI_Val.Focused := false;end; I get an 'Error: Argument cannot be assigned to' This is because Focused is function, not property. Quote from: jamie on June 04, 2022, 03:55:47 pm  You can also test the FOCUS property Test the result of function Focused, to be precise.