Recent

Author Topic: how not to execute onexit  (Read 4230 times)

rcmz

  • Full Member
  • ***
  • Posts: 137
how not to execute onexit
« on: July 15, 2024, 07:12:37 pm »
hi,

in delphi when I push the button to exit and close, it simple executes.

In Lazarus if I have a TEdit with an OnExit and push the close button it first executes de OnExit event and does not execute the close button.

Any ideas are wellcome.

TIA
ramiro

cdbc

  • Hero Member
  • *****
  • Posts: 1668
    • http://www.cdbc.dk
Re: how not to execute onexit
« Reply #1 on: July 15, 2024, 07:56:37 pm »
Hi
Hmmm.... Must admit, I've never had that happen to me, so I'm blank...
What happens if you in the 'ButtonCloseClick' handler, sets the 'OnExit' event-handler to nil, before you call 'Close'?!?
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

tetrastes

  • Hero Member
  • *****
  • Posts: 595
Re: how not to execute onexit
« Reply #2 on: July 15, 2024, 09:09:21 pm »
In Lazarus if I have a TEdit with an OnExit and push the close button it first executes de OnExit event and does not execute the close button.

I don't understand what do you mean. This
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.     Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.     { TForm1 }
  13.  
  14.     TForm1 = class(TForm)
  15.         Button1: TButton;
  16.         Edit1: TEdit;
  17.         procedure Button1Click(Sender: TObject);
  18.         procedure Edit1Exit(Sender: TObject);
  19.     private
  20.  
  21.     public
  22.  
  23.     end;
  24.  
  25. var
  26.     Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. { TForm1 }
  33.  
  34. procedure TForm1.Edit1Exit(Sender: TObject);
  35. begin
  36.     Edit1.Text:='Tedit.OnExit';
  37. end;
  38.  
  39. procedure TForm1.Button1Click(Sender: TObject);
  40. begin
  41.     Close;
  42. end;
  43.  
  44. end.
  45.  

and corresponding unit1.lfm:
Code: Pascal  [Select][+][-]
  1. object Form1: TForm1
  2.   Left = 440
  3.   Height = 240
  4.   Top = 112
  5.   Width = 320
  6.   Caption = 'Form1'
  7.   ClientHeight = 240
  8.   ClientWidth = 320
  9.   LCLVersion = '3.2.0.0'
  10.   object Edit1: TEdit
  11.     Left = 68
  12.     Height = 23
  13.     Top = 58
  14.     Width = 80
  15.     TabOrder = 0
  16.     Text = 'Edit1'
  17.     OnExit = Edit1Exit
  18.   end
  19.   object Button1: TButton
  20.     Left = 72
  21.     Height = 25
  22.     Top = 107
  23.     Width = 75
  24.     Caption = 'Button1'
  25.     TabOrder = 1
  26.     OnClick = Button1Click
  27.   end
  28. end
  29.  

works as expected.

jamie

  • Hero Member
  • *****
  • Posts: 6735
Re: how not to execute onexit
« Reply #3 on: July 15, 2024, 10:53:04 pm »
I cannot reproduce the issue. I started a simple application, dropped an Edit and a Tbutton on it. did the assignments.

All works as expected The app closes.
The only true wisdom is knowing you know nothing

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1225
Re: how not to execute onexit
« Reply #4 on: July 16, 2024, 02:13:26 am »
I have done something like this .
Have you checked if the event for tbutton is assigned?
What will happen is the tedit.onexit Event should be executed first. Then the button click event.

Can you show us the code responsible for handling change events ?
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

Weiss

  • Full Member
  • ***
  • Posts: 185
Re: how not to execute onexit
« Reply #5 on: July 16, 2024, 04:10:28 am »
I have no comp with me, but I always thoughts "onExit" means "on Exit from Component" ?
Doesn't mean "on exit from application". On other hand, if you insert
Code: Pascal  [Select][+][-]
  1. close;
in your "onExit" event for this particular component, TEdit in your case, exiting your TEdit (simply by clicking anywhere else on the form) will close the application.

If you want something happen to your TEdit at the time you close the form (or application) that it will have to happen inside the form's "onExit" event

Weiss

  • Full Member
  • ***
  • Posts: 185
Re: how not to execute onexit
« Reply #6 on: July 16, 2024, 04:16:01 am »
sorry, I meant form's "onClose" event. And I did test, everything works. Beautiful Lazarus works like a charm

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1225
Re: how not to execute onexit
« Reply #7 on: July 19, 2024, 03:04:31 pm »
Onexit likely refers to the edit losing focus. It also seems to get triggered by an editing done event even if tedit is only control on form.
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

rcmz

  • Full Member
  • ***
  • Posts: 137
Re: how not to execute onexit
« Reply #8 on: July 19, 2024, 08:41:04 pm »
Thx

I was wrong, both excute exactly the same.

Is there a way to press the cancel or close button without executing the OnExit event of a TEdit component.

TIA
Ramiro

cdbc

  • Hero Member
  • *****
  • Posts: 1668
    • http://www.cdbc.dk
Re: how not to execute onexit
« Reply #9 on: July 20, 2024, 08:49:13 am »
Hi
Quote
Is there a way to press the cancel or close button without executing the OnExit event of a TEdit component.
I think not, 'cause when you click either button, the edit looses focus and the OnExit event fires...  %) So it doesn't matter if you set the 'OnExit' handler to nil, in the button-click method, it's too dang late...  :P
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1225
Re: how not to execute onexit
« Reply #10 on: July 20, 2024, 02:47:32 pm »
Thx

I was wrong, both excute exactly the same.

Is there a way to press the cancel or close button without executing the OnExit event of a TEdit component.

TIA
Ramiro
You can experiment with having the on exit and editingdone events set to nil. In any case if all controls events are handled in same procedure, you can simply exit if the sender is tedit
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

Zoran

  • Hero Member
  • *****
  • Posts: 1882
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: how not to execute onexit
« Reply #11 on: July 21, 2024, 12:14:42 am »
When you click the button, the button steals the focus from edit. So the first thing that happens is that the edit control loses focus (which triggers OnExit event). Only then button's OnClick event executes. So you cannot prevent OnExit inside button's OnClick code, it's too late.

In any case if all controls events are handled in same procedure, you can simply exit if the sender is tedit

Would not work. If same procedure is assigned to Edit.OnExit and Button.OnClick, this procedure will be executed twice - first time sender is Edit, and the second time sender is Button. In the first call, when sender is edit, you don't know that the button is clicked (you can check which control has focus, but even if it is the button you do not know that it is clicked) and application will close, and in the second call it is too late.
It is same problem as if two separate procedures are used.
« Last Edit: July 21, 2024, 12:18:04 am by Zoran »

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1225
Re: how not to execute onexit
« Reply #12 on: July 22, 2024, 08:37:54 am »
Yes that’s true if both the onexit and oneditingdone events are assigned, each one of them with have events happen for the same action. Which of course is rather unnecessary. I only use the oneditingdone done event for tedit and I process events for all controls in the same event procedure. If you do this and put a breakpoint inside your event procedure you will see that the event procedure is called first by editing done event and then by button click event. So you should be able to process the button click to close window.
I think it’s good to have the editingdone event fire before the button click to close form because it gives you a chance to preserve the contents of the teditcontrol to an inifile for restoring next time program starts.
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

Zoran

  • Hero Member
  • *****
  • Posts: 1882
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: how not to execute onexit
« Reply #13 on: July 24, 2024, 09:36:58 am »
Yes that’s true if both the onexit and oneditingdone events are assigned, each one of them with have events happen for the same action. Which of course is rather unnecessary. I only use the oneditingdone done event for tedit and I process events for all controls in the same event procedure. If you do this and put a breakpoint inside your event procedure you will see that the event procedure is called first by editing done event and then by button click event. So you should be able to process the button click to close window.
I think it’s good to have the editingdone event fire before the button click to close form because it gives you a chance to preserve the contents of the teditcontrol to an inifile for restoring next time program starts.

What do you mean? Edit.OnEditingDone fires even before Edit.OnExit, so obviously before Button.OnClick. So Button.OnClick still fires too late, which is the problem.

Zvoni

  • Hero Member
  • *****
  • Posts: 2747
Re: how not to execute onexit
« Reply #14 on: July 24, 2024, 12:18:00 pm »
Seems like something like a "race"-condition (not the best term for that).
Meaning: The code in his OnExit-Event is probably in the wrong place.

e.g. Let's say he has 4 TEdit's on his Form, and he has Code in OnExit for the first TEdit, which executes if he tabs/clicks to the second TEdit and so on.
But he wants to ignore that Code in TEdit1.OnExit, if he presses a Button (Cancel/Abort?)

To me clear indicator, the Code in OnExit is in the wrong place
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

 

TinyPortal © 2005-2018