List of indices out of bounds in TPageControl

I am stuck with TPageControl, which exhibits strange behavior.

The control has 3 pages, but when I do

for I:=0 to PageControl.PageCount-1 do begin PageControl.Pages[I].TabVisible := False; PageControl.Pages[I].Visible := Ord(iColorScale.GenerationMode) = I; end; 

I get an "index index beyond (3)" error when executing the first line of the first iteration of the loop equivalent

 PageControl.Pages[0].TabVisible := False; 

Now when I look at the PageControl properties in the debugger, everything seems to be in order. The PageCount value is expected to be 3, and I will see all pages and their properties, including TabVisible page 0, in the evaluation document

I am using Delphi XE on a Windows 7 machine. Does anyone have an idea what is going on? I'm at a loss.

+7
delphi delphi-xe tpagecontrol
source share
1 answer

TL; DR: set PageControl.HandleNeeded before setting TabVisible .

There is a good explanation here (Greg Chapman): TabVisible on table tables and indexes
For reference SO (copy / paste) SO:

If the PageControl descriptor has been destroyed (which can happen if setting any property in PageControl or any of its parent windows calls RecreateWnd ), PageControl saves visible tabs to TStringList ( FSaveTabs ). Setting TabVisible this procedure:

 procedure TTabSheet.SetTabShowing(Value: Boolean); var Index: Integer; begin if FTabShowing <> Value then if Value then begin FTabShowing := True; FPageControl.InsertTab(Self); end else begin Index := TabIndex; FTabShowing := False; FPageControl.DeleteTab(Self, Index); end; end; 

During a call to FPageControl.DeleteTab , PageControl will update its handle if necessary. In doing so, he tries to use reset visible tabs using FSaveTabs . However, this can be confusing because one of the tabs added to FSaveTabs is now invisible ( TabSheet.FTabShowing = false ). This raises an IndexError. Thus, the fix is ​​to make sure that the pen is recreated before installing TabVisible .

+7
source share

All Articles