Search code examples
windowsdelphidelphi-xe2tpagecontrol

TPageControl get index


How do I get the number of the page where there is an object? For example: there is a Button1 on the first page and Button2 on the second page, how do I get the number of the page where there is the Button1 without ActivePageIndex? Thanks.


Solution

  • It's very common to want to find the closest parent of a specific class. So, it pays dividends to make a function to do just that.

    function GetParentWithClass(Control: TControl; 
      ClassType: TWinControlClass): TWinControl;
    begin
      Result := Control.Parent;
      while Assigned(Result) and not (Result is ClassType) do
        Result := Result.Parent;
    end;
    

    Once you have this in place you can use it to solve your current problem.

    var
      PageIndex: Integer;
      TabSheet: TTabSheet;
    .....
    TabSheet := GetParentWithClass(Control, TTabSheet) as TTabSheet;
    PageIndex := TabSheet.PageIndex;
    

    Having separated the concerns like this you can make use of GetParentWithClass in other settings.