View Issue Details

IDProjectCategoryView StatusLast Update
0031028LazarusLCLpublic2016-12-21 14:10
ReporterJean Patrick Assigned Towp  
Status closedResolutionfixed 
Product Version1.6.2 
Target Version1.6.4Fixed in Version1.7 (SVN) 
Summary0031028: Error when using component SetFocus property in an OnShow event of a TabSeet
DescriptionThe error that occurred is displayed in the attached image (Img001.jpg).
Also, I have attached a simple example (
Tags1.6.2, lcl
Fixed in Revision51631
Attached Files


Jean Patrick

2016-11-28 17:29

reporter (2,682 bytes)

Jean Patrick

2016-11-28 17:30


Img001.jpg (44,875 bytes)   
Img001.jpg (44,875 bytes)   


2016-11-28 18:54

developer   ~0096355

Probably the edit control is not yet visible when the tab's OnShow fires.

For all the occasions that a control must be focussed before it is shown the Form has the property ActiveControl. If you set this property to the PageControl then the focus does not move to the edit controls when you activate another tab:

procedure TForm1.TabSheet1Show(Sender: TObject);
// EditTab1.SetFocus;
  ActiveControl := PageControl1;

procedure TForm1.TabSheet2Show(Sender: TObject);
// EditTab2.SetFocus;
  ActiveControl := PageControl1;

(Of course you should use the same event handler for both tabs).

Please confirm that this works, and I'll close the report as "no change required".

Jean Patrick

2016-11-29 02:58

reporter   ~0096369

Without even changing tabs, the error already occurs.
The error occurs in EditTab1.SetFocus that is on the visible tab, when the form is displayed.
It does not resolve to define "ActiveControl = PageControl1".

I have found that TForm, by default, sets "Visible = False". When setting, in the ObjectInspector, "Visible = True" the error is resolved.


2016-11-29 09:10



2016-11-29 09:14

developer   ~0096377

Maybe I don't understand correctly what you are saying. But I cannot reproduce this behavior. Please run the attached demo (PageControl_TestTabs_issue31028, based on yours): Immediately after the form is shown the focus is on the PageControl, not on Edit1.

Never call AControl.SetFocus if you are not sure that the control is visible. Use Form.ActiveControl := AControl instead.

Jean Patrick

2016-12-19 20:27

reporter (4,503 bytes)

Jean Patrick

2016-12-19 20:27


patch.patch (471 bytes)   
*** C:/	Mon Oct 10 21:22:57 2016
--- C:/	Mon Dec 19 15:40:12 2016
*** 2877,2887 ****
      Include(FFormState, fsModal);
-     if PopupMode = pmNone then
-     begin
-       RecreateWnd(Self);
-       HandleNeeded;
-     end;
      ActiveWindow := GetActiveWindow;
      SavedFocusState := SaveFocusState;
      SavedCursor := Screen.Cursor;
--- 2877,2882 ----
patch.patch (471 bytes)   

Jean Patrick

2016-12-19 20:29

reporter   ~0096940

I refined the example, because the previous one did not show the difference between version 1.6.2 and 1.6.0.

The error occurs when a modal form, with a PageControl component and OnShow events of the Tabs implemented with SetFocus instruction of some component (TEdit, for example), is called by the main form (see new attached example: New_TestTabs).

In Lazarus 1.6.0 the problem does not occur, because we do not have the change made in the customform.ini file of version 1.6.2 (see patch attachment: patch.patch).

Due to this change in version 1.6.2, we have that the bug can be bypassed by changing the PopupMode property of modal form to a value other than pmNone. But this is not the ideal.


2016-12-20 18:14

developer   ~0096957

Thanks. Now I see the issue.

Digging in the svn commits I found that this issue was fixed in r51631 by Ondrej - that's why I don't see it with Laz trunk. The fix will be backported to the fixes branch.

I think you can test the fix easily on your 1.6.2 version:

* Open (in lcl/include)
* Find the method CustomForm.ShowModal
* At the beginning of the first try-finally block (near line 2880) replace the

    if PopupMode = pmNone then


    if (PopupMode = pmNone) and HandleAllocated then

Please test and close if ok, otherwise re-create.

Jean Patrick

2016-12-21 14:10

reporter   ~0096995

Tested and all ok. Situation corrected.

Issue History

Date Modified Username Field Change
2016-11-28 17:29 Jean Patrick New Issue
2016-11-28 17:29 Jean Patrick File Added:
2016-11-28 17:30 Jean Patrick File Added: Img001.jpg
2016-11-28 17:35 Jean Patrick Tag Attached: 1.6.2
2016-11-28 17:35 Jean Patrick Tag Attached: lcl
2016-11-28 18:54 wp Note Added: 0096355
2016-11-28 18:54 wp Assigned To => wp
2016-11-28 18:54 wp Status new => assigned
2016-11-28 18:54 wp LazTarget => -
2016-11-28 18:54 wp Status assigned => feedback
2016-11-29 02:58 Jean Patrick Note Added: 0096369
2016-11-29 02:58 Jean Patrick Status feedback => assigned
2016-11-29 09:10 wp File Added:
2016-11-29 09:14 wp Note Added: 0096377
2016-11-30 11:49 wp Status assigned => feedback
2016-12-19 20:27 Jean Patrick File Added:
2016-12-19 20:27 Jean Patrick File Added: patch.patch
2016-12-19 20:29 Jean Patrick Note Added: 0096940
2016-12-19 20:29 Jean Patrick Status feedback => assigned
2016-12-20 18:14 wp Fixed in Revision => 51631
2016-12-20 18:14 wp LazTarget - => 1.6.4
2016-12-20 18:14 wp Note Added: 0096957
2016-12-20 18:14 wp Status assigned => resolved
2016-12-20 18:14 wp Fixed in Version => 1.7 (SVN)
2016-12-20 18:14 wp Resolution open => fixed
2016-12-20 18:14 wp Target Version => 1.6.4
2016-12-21 14:10 Jean Patrick Note Added: 0096995
2016-12-21 14:10 Jean Patrick Status resolved => closed