View Issue Details

IDProjectCategoryView StatusLast Update
0033720LazarusLCLpublic2019-01-04 11:21
ReporterBłażej Roszkowski Assigned ToBart Broersma  
Status resolvedResolutionfixed 
Target Version2.0Fixed in Version2.0 
Summary0033720: TTabControl/TCustomTabControl OnChanging triggers from code but shouldn't
DescriptionThe OnChange event used to be triggered by code but it now isn't.
This is explained by change in 1.8.x to be compatible with Delphi:

The problem is that OnChanging still triggers so it leads to weird situation where I get that event (to save stuff in current tab in) but then don't get an OnChange (to change tab contents to new stuff) after it.

I could reproduce it on an old (24-25-ish, I can't recall) and new Fedora 28 and on an old and fresh installation Windows 10 with 1.8.2 (using Gtk2 and WinAPI LCL respectively).

I believe neither should trigger by default and both should if the flag in Options is set.

Delphi Tokyo docs use same wording in 'Note' about in code triggering for both events:

I'm not sure if I'm right since I'm very weak at Delphi/Pascal but this came up in this thread and wp has told me to report this (ignore posts by jamie):,41164.msg285177.html

I also don't own a copy of any modern Delphi and I don't want to register and use up my trial period for this so I can't check it myself easily.
TagsNo tags attached.
Fixed in Revisionr59985
Attached Files


related to 0025554 resolvedBart Broersma pagecontrol onchange event 


Vojtech Cihak

2018-05-10 13:42

reporter   ~0108225

I just tested: old Delphi 7 does not trigger OnChanging event when TabIndex is changed by code.


2018-05-10 14:23

developer   ~0108226

Forgotten when the OnChange event was fixed (issue 0025554)?

Bart Broersma

2018-05-10 19:55

developer   ~0108233

Yeah, probably (so, my fault then).

Bart Broersma

2019-01-02 11:54

developer   ~0113089

OnChanging is called in CanChange, which unfortunately is also called when user changes tabs by clicking with the mouse.

Bart Broersma

2019-01-02 12:22

developer   ~0113090

CanChange is called automatically when an attempt is made to change the selected tab. CanChange generates an OnChanging event so that an OnChanging event handler can prevent the selection from changing. If there is no OnChanging event handler, CanChange returns true, allowing the change.

Does that mean that CanChange is NOT called when Tab is changed by code?

Bart Broersma

2019-01-03 22:19


tabcontrol.onchanging.diff (3,573 bytes)   
Index: lcl/comctrls.pp
--- lcl/comctrls.pp	(revision 59933)
+++ lcl/comctrls.pp	(working copy)
@@ -463,7 +463,7 @@
     procedure InsertPage(APage: TCustomPage; Index: Integer); virtual;
     procedure RemovePage(Index: Integer); virtual;
   //Delphi compatible properties
-    function CanChange: Boolean; virtual;
+    function CanChange(User: Boolean = True): Boolean; virtual;
     property DisplayRect: TRect read GetDisplayRect;
     property HotTrack: Boolean read FHotTrack write FHotTrack default False;
     property MultiSelect: Boolean read FMultiSelect write FMultiSelect default False;
@@ -481,7 +481,7 @@
     function GetImageIndex(ThePageIndex: Integer): Integer; virtual;
     function IndexOf(APage: TPersistent): integer; virtual;
     function CustomPage(Index: integer): TCustomPage;
-    function CanChangePageIndex: boolean; virtual;
+    function CanChangePageIndex(User: Boolean = True): boolean; virtual;
     function GetMinimumTabWidth: integer; virtual;
     function GetMinimumTabHeight: integer; virtual;
     function GetCapabilities: TCTabControlCapabilities; virtual;
@@ -821,7 +821,7 @@
     procedure SetOptions(const AValue: TCTabControlOptions); override;
     procedure AddRemovePageHandle(APage: TCustomPage); override;
-    function CanChange: Boolean; override;
+    function CanChange(User: Boolean = True): Boolean; override;
     function CanShowTab(ATabIndex: Integer): Boolean; virtual;
     procedure Change; override;
     procedure CreateWnd; override;
Index: lcl/include/
--- lcl/include/	(revision 59933)
+++ lcl/include/	(working copy)
@@ -389,15 +389,16 @@
-function TCustomTabControl.CanChangePageIndex: boolean;
+function TCustomTabControl.CanChangePageIndex(User: Boolean = True): boolean;
-  Result := CanChange;
+  Result := CanChange(User);
-function TCustomTabControl.CanChange: Boolean;
+function TCustomTabControl.CanChange(User: Boolean = True): Boolean;
   Result := True;
-  if ([csDesigning, csDestroying] * ComponentState = []) and Assigned(OnChanging) then
+  if ([csDesigning, csDestroying] * ComponentState = []) and Assigned(OnChanging)
+    and (User or (nboDoChangeOnSetIndex in Options)) then
     OnChanging(Self, Result);
@@ -581,7 +582,7 @@
   if (AValue < -1) or (AValue >= PageCount) then Exit;
   //debugln('TCustomTabControl.SetPageIndex A ',dbgsName(Self),' AValue=',dbgs(AValue),' fPageIndex=',dbgs(fPageIndex),' PageCount=',dbgs(PageCount),' HandleAllocated=',dbgs(HandleAllocated),' ',dbgs(ComponentState));
   if FPageIndex = AValue then exit;
-  if not CanChangePageIndex then exit;
+  if not CanChangePageIndex(False) then exit;
   //debugln('TCustomTabControl.SetPageIndex B ',dbgsName(Self),' AValue=',dbgs(AValue),' fPageIndex=',dbgs(fPageIndex),' PageCount=',dbgs(PageCount),' HandleAllocated=',dbgs(HandleAllocated));
Index: lcl/include/
--- lcl/include/	(revision 59933)
+++ lcl/include/	(working copy)
@@ -558,7 +558,7 @@
   // There are no pages, don't create a handle
-function TTabControl.CanChange: Boolean;
+function TTabControl.CanChange(User: Boolean = True): Boolean;
   if FTabControlCreating then exit;
tabcontrol.onchanging.diff (3,573 bytes)   

Bart Broersma

2019-01-03 22:20

developer   ~0113138

Possible patch attached, which unfortunately is not Delphi compatible, since it changes the signature of CanChange().

Bart Broersma

2019-01-04 11:21

developer   ~0113151

Thanks for reporting.
Please test and close if OK.

Issue History

Date Modified Username Field Change
2018-05-10 12:10 Błażej Roszkowski New Issue
2018-05-10 13:42 Vojtech Cihak Note Added: 0108225
2018-05-10 14:21 wp Relationship added related to 0025554
2018-05-10 14:23 wp Note Added: 0108226
2018-05-10 19:55 Bart Broersma Note Added: 0108233
2019-01-02 11:54 Bart Broersma Note Added: 0113089
2019-01-02 12:22 Bart Broersma Note Added: 0113090
2019-01-02 12:35 Bart Broersma Assigned To => Bart Broersma
2019-01-02 12:35 Bart Broersma Status new => assigned
2019-01-03 22:19 Bart Broersma File Added: tabcontrol.onchanging.diff
2019-01-03 22:20 Bart Broersma Note Added: 0113138
2019-01-04 11:21 Bart Broersma Fixed in Revision => r59985
2019-01-04 11:21 Bart Broersma LazTarget => 2.0
2019-01-04 11:21 Bart Broersma Note Added: 0113151
2019-01-04 11:21 Bart Broersma Status assigned => resolved
2019-01-04 11:21 Bart Broersma Fixed in Version => 2.0
2019-01-04 11:21 Bart Broersma Resolution open => fixed
2019-01-04 11:21 Bart Broersma Target Version => 2.0