TDateTimePicker calls FormActivate every time the date is changed lazarus / 31-LCL ---------------------------------------------------------------------- JD · 2021-09-22 15:18 Hi there everyone, I have noticed a strange occurence with the TDateTimePicker control and FormActivate. Everytime one changes the date in TDateTimePicker, FormActivate is called. Is this normal behaviour? How do I get around it because it is causing one of my applications to behave strangely? 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, DateTimePicker; type  { TForm1 }  TForm1 = class(TForm)    DateTimePicker1: TDateTimePicker;    procedure FormActivate(Sender: TObject);  private   public   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.FormActivate(Sender: TObject);begin  //if Sender is TDateTimePicker then    ShowMessage('Form activate called!');end; end.  JD ---------------------------------------------------------------------- wp · 2021-09-22 16:14 Quote from: JD on September 22, 2021, 05:18:05 pmEverytime one changes the date in TDateTimePicker, FormActivate is called. Is this normal behaviour? I think so. Because the dropdown calendar is in its own form which is shown non-modally. The same occurs for TDateEdit and TTimeEdit. Whenever a non-modal form is activated/deactivated the OnActivate and OnDeactivate events are fired. And this happens when these dropdowns are closed - now the parenting form becomes active again and fires the OnActivate event. TCalcEdit and TDirectoryEdit do not show this behaviour - here the "dropdown" is a separate form, too, but it is a shown modally. When focus returns to the initial form after closing a modal form the OnActivate event does not fire. Maybe the TColorPicker, TDateEdit and TTimeEdit can be rewritten to show the dropdowns modally (I have no idea about the implications) or to use custom controls rather than forms for their dropdowns - but all these require a major rework in these components. You should reconsider the logic of your event handling. For example you can provide a boolean "ActivateCalled" status variable which you set to true the first time when the form gets activated and thus can prevent execution of the code in the OnActivate handler multiple times (https://wiki.freepascal.org/Execute_action_after_form_is_shown#OnActi vate_event). ---------------------------------------------------------------------- Zoran · 2021-09-22 16:17 Quote from: JD on September 22, 2021, 05:18:05 pm Everytime one changes the date in TDateTimePicker, FormActivate is called. Is this normal behaviour? You mean, when the drop-down calendar in DateTimePicker gets closed, the parent's form's OnActivate handler is called. Right? It is a known problem, but not much can be done in TDatePicker to avoid it. The problem is that the drop-down calendar, when shown, actually creates its own form and it steals the focus from DateTimePicker's parent form. When the calendar form is closed, the DateTimePicker's parent form gets the focus back. Then the activate event is triggered. It was discussed once. ---------------------------------------------------------------------- balazsszekely · 2021-09-22 16:23 Hi JD, QuoteI have noticed a strange occurence with the TDateTimePicker control and FormActivate. Everytime one changes the date in TDateTimePicker, FormActivate is called. Is this normal behaviour?Yes, it is normal behaviour. Take a look at the attached picture, it's clearly visible with Window detective, that TDateTimePicker has a separate window(Form). QuoteHow do I get around it because it is causing one of my applications to behave strangely?You can use TScreen class to detect when the active form is a dropped down TDateTimePicker, then change the behaviour on form activate: 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)    DateTimePicker1: TDateTimePicker;    procedure FormActivate(Sender: TObject);    procedure FormCreate(Sender: TObject);    procedure FormDestroy(Sender: TObject);  private     FCalendarIsActive: Boolean;     procedure OnAddForm(Sender: TObject; Form: TCustomForm);     procedure OnRemoveForm(Sender: TObject; Form: TCustomForm);  public   end;  //... procedure TForm1.FormCreate(Sender: TObject);begin  Screen.AddHandlerFormAdded(@OnAddForm);  Screen.AddHandlerRemoveForm(@OnRemoveForm);end; procedure TForm1.FormDestroy(Sender: TObject);begin  Screen.RemoveHandlerFormAdded(@OnAddForm);  Screen.RemoveHandlerRemoveForm(@OnRemoveForm);end; procedure TForm1.OnAddForm(Sender: TObject; Form: TCustomForm);begin  FCalendarIsActive := Form.ClassName = 'TDTCalendarForm';end; procedure TForm1.OnRemoveForm(Sender: TObject; Form: TCustomForm);begin  FCalendarIsActive := False; //called after Form1 FormActivateend; procedure TForm1.FormActivate(Sender: TObject);begin  if FCalendarIsActive then //called first  begin    ShowMessage('we closed the calendar');    Exit;  end  else  begin    // do other non calendar related processing  end;end; PS: Tested on Windows 10, Lazarus Trunk, FPC 3.2.2. It should work on other widgetsets too.