Jump to content
Sign in to follow this  
Henry Olive

Re-Index a PageControl.PageIndex

Recommended Posts

I wish everyone a healthy day.

My PageControl looks like below

TabSheet1-TabSheet2-TabSheet3-TabSheet4-TabSheet5-TabSheet6.......

In some case i need to make TabSheet3 and/or TabSheet4  Invisible

I wrote below codes

TabSheet3.TabVisible := CDS1UserRight1.asString ='Y'; //User has no Right for this Tab so it is UNvisible
TabSheet4.TabVisible := CDS1UserRight2.asString ='Y';  //User has no Right for this Tab so it is UNvisible

In this case i need to re-arrange the PageControl1.PageIndexes so i wrote below codes

for i:=0 to PC1.PageCount-1 do
begin
   if PC1.Pages.TabVisible=True then
      PC1.Pages.PageIndex := i;
end; 

Above codes works w/o any errror but

I expected to see TabSheet1-TabSheet2-OLDTabSheet5(New TabSheet3) -OLDTabSheet6(NewTabSheet4)

**Contents of TabSheets are correct**  but their captions are wrong ( I see Invisible TabSheets Captions)

What am i doing  wrong ?

 

Thank You
 

 

 

 

 

 

 

 

 

 

 

 

 

Edited by Sherlock
Please use code tags

Share this post


Link to post
30 minutes ago, Henry Olive said:

if PC1.Pages.TabVisible=True then
      PC1.Pages.PageIndex := i;

Sorry, but that code doesn't make sense. Pages is an array property.

 

Why exactly do you need to change the PageIndex? After all each TabSheet also has a TabIndex property, which gives the index in the visible tabs or -1 if the tab is invisible.

Share this post


Link to post

Thank you so much Uwe for your reply.

First I'm so sorry for my poor english may be i cant express my problem good.

My PageControl's owner draw = True;

May be problem occurs from this proc. // Below proc it self works w/o any problem
 

procedure TMainForm.PC1DrawTab(Control: TCustomTabControl;
  TabIndex: Integer; const Rect: TRect; Active: Boolean);
var
  XTabSheet: TTabSheet;
  XRect: TRect;
  FormColor: String;
begin
  if Active  then
  begin
    Control.Canvas.Brush.Color:=$006ED7FC;
    Control.Canvas.Font.Color:=clBlack;
  end else
  begin
    FormColor :=dm.SettingsFormColor.AsString;

    if MatchText(FormColor,['Windows','Gray']) then
    Control.Canvas.Brush.Color:=$003C3838 else 
    if MatchText(FormColor,['Brown','Light Brown']) then
    Control.Canvas.Brush.Color:=$0034527E;

......
  end;
  XRect:=Rect;
  Control.Canvas.FillRect(XRect);
  XTabSheet:=PC1.Pages[TabIndex];
  DrawText(Control.Canvas.Handle,
  PChar(XTabSheet.Caption), length(XTabSheet.Caption),
  XRect, DT_VCENTER + DT_SINGLELINE + DT_CENTER);
end;


 

Lets say i have a PageControl and 4 Tabsheets

When i try to make INVISIBLE  TabSheet3 and Tabsheet4  *i have no problem*  (they are last tabsheets !!)

but when i make invisible   Tabsheet2 and Tabsheet3 ( In RunTime )

Eventhough I expect to see Tabsheet1 - TabSheet4 (TabSheet4's caption is still Tabsheet2 but contents are Correct )  

 

Thank You
 

 

 

 

Edited by Sherlock
Please use code tags

Share this post


Link to post

I think you need to write a short but complete program that demonstrates the issue. Otherwise it requires us to guess which is not effective or efficient. 

Share this post


Link to post

If you have invisible tabsheets you cannot write anymore:  XTabSheet:=PC1.Pages[TabIndex];

You've to find in a pages the index that contains the TabSheet with Tabindex. Something like this:

procedure TMainForm.PC1DrawTab(Control: TCustomTabControl;
  TabIndex: Integer; const Rect: TRect; Active: Boolean);
var
  XTabSheet: TTabSheet;
  XRect: TRect;
  FormColor: String;
  lpc: TPageControl;
  i: integer;
begin
  if Active  then
  begin
    Control.Canvas.Brush.Color:=$006ED7FC;
    Control.Canvas.Font.Color:=clBlack;
  end else
  begin
    FormColor :=dm.SettingsFormColor.AsString;

    if MatchText(FormColor,['Windows','Gray']) then
    Control.Canvas.Brush.Color:=$003C3838 else 
    if MatchText(FormColor,['Brown','Light Brown']) then
    Control.Canvas.Brush.Color:=$0034527E;

......
  end;
  XRect:=Rect;
  Control.Canvas.FillRect(XRect);
  //XTabSheet:=PC1.Pages[TabIndex];
  lpc:=Control as Tpagecontrol;
  XTabSheet:=nil;
  i:=0;
  while (XTabSheet=nil) and (i<lpc.PageCount) do
  begin
    if lpc.pages[i].TabIndex=TabIndex then
      XTabSheet:=lpc.pages[i];
    inc(i);
  end;
    
  DrawText(Control.Canvas.Handle,
  PChar(XTabSheet.Caption), length(XTabSheet.Caption),
  XRect, DT_VCENTER + DT_SINGLELINE + DT_CENTER);
end;

 

Share this post


Link to post
23 hours ago, Henry Olive said:

..


TabSheet3.TabVisible := CDS1UserRight1.asString ='Y'; //User has no Right for this Tab so it is UNvisible
TabSheet4.TabVisible := CDS1UserRight2.asString ='Y';  //User has no Right for this Tab so it is UNvisible

 In this case i need to re-arrange the PageControl1.PageIndexes so i wrote below codes


for i:=0 to PC1.PageCount-1 do
begin
   if PC1.Pages.TabVisible=True then
      PC1.Pages.PageIndex := i;
end; 

Above codes works w/o any error but

I expected to see TabSheet1-TabSheet2-OLDTabSheet5(New TabSheet3) -OLDTabSheet6(NewTabSheet4)

**Contents of TabSheets are correct**  but their captions are wrong ( I see Invisible TabSheets Captions)

What am i doing  wrong ?

Thank You

You do not need to modify the PageIndex as Uwe mentions.  Since you didn't change the tabIndex at that modification, the pagecontrol is giving out of synch results.  

 

Simply remove the for loop changing the pageIndex. Its wrong.

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×