5

I'm using a TPageControl, where certain pages are not visible.

This breaks the normal 1:1 mapping of the TabIndex and ActivePageIndex properties.

In most places I can use ActivePageIndex (or ActivePage itself) to get the current page, but I want a dynamic tooltip hint which requires me to determine which page is associated with a specific tab index.

If I call pageControl.IndexOfTabAt(X, Y), I get a Tab Index back, but I can't directly use that as an index into the Pages[] array, as some page tabs aren't visible.

I could explicity iterate through the pages, ignoring the visible ones, but it feels like there should be something in the VCL that does this for me already...?

Roddy
  • 66,617
  • 42
  • 165
  • 277
  • Why do you need "to determine which page is associated with a specific tab index"? – Leonardo Herrera Jan 26 '10 at 13:33
  • @Leonardo: I'm dynamically creating pages and each page has a bunch of data members. Each tab has a dynamically generated "hint" extracted from the data, set by the PageControl onMouseMove(X,Y) which then calls IndexOfTabAt(X,Y) to determine which tab the mouse is over. Enough? – Roddy Jan 26 '10 at 14:11

3 Answers3

2

I you look in the source for TPageControl (ComCtrls.pas), there is a private method:

function TPageControl.PageIndexFromTabIndex(TabIndex: Integer): Integer;

that does what you want. But you can't call it (D2007), so (unfortunately) you have to copy the code.

BennyBechDk
  • 934
  • 7
  • 13
  • 2
    Not only can you not call it, it doesn't even work right in many cases! http://qc.embarcadero.com/wc/qcmain.aspx?d=30263 – Roddy Jan 26 '10 at 14:25
0

This version seems to work:

function PageIndexFromTabIndex(const pageControl : TPageControl; const TabIndex: Integer): Integer;
 var
    i : Integer;
 begin
   Result := TabIndex;
   for i := 0 to Pred(pageControl.PageCount) do
     begin
       if not pageControl.Pages[i].TabVisible then
         Inc(Result);
       if TabIndex = pageControl.Pages[i].TabIndex then
         break;
     end;
  end;
-1

Here is an old article that deals with dragging & dropping pages. It has some logic to obtain the index of a page from an (X, Y) position, perhaps you can use it. Something like this (untested):

function TMyPageControl.GetPageIndexAtPos(X, Y: Integer) : Integer;
const
   TCM_GETITEMRECT = $130A;
var
   TabRect: TRect;
   j: Integer;    
begin
   for j := 0 to PageCount - 1 do
   begin
     Perform(TCM_GETITEMRECT, j, LParam(@TabRect)) ;
     if PtInRect(TabRect, Point(X, Y)) then
     begin
       Result := j;
       exit;
     end;
   end;
   Result := -1;
end;
Leonardo Herrera
  • 8,388
  • 5
  • 36
  • 66