[SOLVED] What is the Sender when 'X' close button is clicked in the title bar? lazarus / 31-LCL ---------------------------------------------------------------------- fatmonk · 2019-09-25 14:22 I'm trying to differentiate between a user clicking the X close button and selecting a File->Exit menu option. To try to figure things out I have: Code: [Select]procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: boolean); begin   if (Sender = miExit) then     showMessage('Exit from menu')     else      showMessage('Exit from elsewhere');   CanClose:=true;  end; But whether I click the 'X' or the 'Exit' menu item I always get the message 'Exit from elsewhere'. Am I checking the Sender in the right place and what should I be checking for to detect a click on the 'X' only (rather than that being the else part of the conditional). -FM ---------------------------------------------------------------------- Thaddy · 2019-09-25 14:33 In your case it is Mainform1 or something. The sender is always a class.... ---------------------------------------------------------------------- fatmonk · 2019-09-25 14:47 Hmm, I've now got: Code: [Select]  if (Sender = MainMenu) then     showMessage('Exit from menu')   else if (Sender = MainForm) then     showMessage('Exit from X')   else      showMessage('Exit from elsewhere'); and if I always get 'Exit from X'. MainForm is my TMainForm and MainMenu is my TMainMenu. As before I have also tried with miExit (which is a TMenuItem in place of MainMenu but still alwys get the result 'Exit from X'. So my code doesn't seem to differentiate between the menu and the X still. -FM ---------------------------------------------------------------------- howardpc · 2019-09-25 15:13 Sender will not differentiate, since the Close message is always relayed by the enclosing form, so the Sender does not vary, it does not reflect the original user trigger. You have to adopt a different approach. 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  Forms, Dialogs, Menus; type    TForm1 = class(TForm)     MainMenu1: TMainMenu;     miExit: TMenuItem;     MenuItemFile: TMenuItem;     procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);     procedure miExitClick(Sender: TObject);   private     CloseByMenuClick: Boolean;   end; var   Form1: TForm1; implementation {$R *.lfm} procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: boolean);begin  case CloseByMenuClick of    False: ShowMessage('Exit from elsewhere');    True:  ShowMessage('Exit from menu');  end;  CanClose:=true;end; procedure TForm1.miExitClick(Sender: TObject);begin  CloseByMenuClick := True;  Close;end; end. ---------------------------------------------------------------------- fatmonk · 2019-09-25 15:38 Thanks Howard, I was holding off heading down that kind of route because I guessed (incorrectly) that there must be a built in way of differentiating. I feel like I'm a big under-user of Sender and this seemed like an ideal use, but I'll flag it in a var in the menu click. Thanks Dan ---------------------------------------------------------------------- lucamar · 2019-09-25 16:20 Quote from: fatmonk on September 25, 2019, 05:38:18 pmI feel like I'm a big under-user of Sender and this seemed like an ideal use, but I'll flag it in a var in the menu click. In my experience (and it's a long and fruitful one) Sender is "underused" almost universally. I use it almost exclusively to differentiate between normal and direct calls to event handlers (say, when you call SomeButtonClick(nil) from some function) or to differentiate, for example, between clicking a button, a menu item or executing an action (which is rarely needed), or similar use-cases. The important thing to remember in those cases is which controls can invoke a common handler, that is with which controls you.can make: 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 TheControl.OnSomeEvent := @AnEventHandler; In your case, OnCloseQuery is an event of forms only, so Sender is always the form itself (unless called directly, of course ... though that makes usually little sense) and you have to rely on workarounds like the ones explained above. ---------------------------------------------------------------------- winni · 2019-09-25 16:27 Hi fatmonk! You can have a look at the source and you'll see that alle the components just call TForm1.Close. Without a sender or any other prameters. If you place your cursor on close and hit Alt - CursorUp you will be moved to procedure TCustomForm.Close; You can see there are a lot of different cases are handled (modal, MDI, CanClose) and the main issue: is this the MainForm. If yes then  Application.Terminate. But nowhere is too see, who called the close procedure. Winni ---------------------------------------------------------------------- winni · 2019-09-25 16:48 Hi! Lucamar's way is one and a good trick to work with the sender. Another one is to simlify and reduce code. Assuming we have thre Trackbars for the colors red, gree, blue. Numbered as Tackbar1,~2,~3. Now  you write a procedure for alle three trackbars when they change. Then you link this procedure to the OnChange event of all 3 trackbars. That is the procedure: 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.TrackBarAllChange(Sender: TObject);var ch: char;beginch := copy (TTrackbar(Sender).name, 9,1);case ch of'1' : ......'2' : ......'3' : ......end;end; So you now have only one procedure instead of three. Winni ---------------------------------------------------------------------- Zvoni · 2019-09-26 08:26 If it's MS Windows, you could always try to catch the Window-Message, check for WM_SYSCOMMAND, and there for SC_CLOSE https://docs.microsoft.com/en-us/windows/win32/menurc/wm-syscommand ---------------------------------------------------------------------- jamie · 2019-09-26 20:48 Why not simply set a TAG property in the menu or somewhere that indicates how it got there ?  If the TAG isn't set then you know it wasn't done by the menu.