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.
Asked
Active
Viewed 753 times
5 Answers
7
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.

David Heffernan
- 601,492
- 42
- 1,072
- 1,490
2
Function GetPageIndexOfControl(c:TControl):Integer;
begin
Result := -1;
While Assigned(c.Parent) and not (c is TTabsheet) do c := c.Parent;
if Assigned(c) then
if c is TTabsheet then
Result := TTabsheet(c).PageIndex;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Showmessage(IntToStr(GetPageIndexOfControl(Button1)));
end;

bummi
- 27,123
- 14
- 62
- 101
2
Try this:
procedure TForm1.Button1Click(Sender: TObject);
begin
if Button1.Parent is TTabSheet then
ShowMessage(IntToStr(TTabSheet(Button1.Parent).PageIndex));
end;
It works if the button is directly in the tab sheet (no panels or other controls). If you have panels, you can walk the parent chain to see if a parent is a TTabSheet:
procedure TForm1.Button1Click(Sender: TObject);
var
Ctrl: TWinControl;
begin
Ctrl := Button1.Parent;
while Assigned(Ctrl) do
begin
if Ctrl is TTabSheet then
begin
ShowMessage(IntToStr(TTabSheet(Ctrl).PageIndex));
Break;
end
else
Ctrl := Ctrl.Parent;
end;
end;

jachguate
- 16,976
- 3
- 57
- 98
2
I like to use Generics
to avoid repeating
PageIndex := TComponentUtil.GetParent<TTabSheet>( MyControl ).PageIndex;
with such a class
unit ComponentUtils;
interface
uses
Classes, Controls;
type
TComponentUtil = class abstract
class function GetOwner<T : TComponent>( AComponent : TComponent ) : T;
class function GetParent<T : TWinControl>( AControl : TControl ) : T;
end;
implementation
{ TComponentUtil }
class function TComponentUtil.GetOwner<T>( AComponent : TComponent ) : T;
var
LOwner : TComponent;
begin
LOwner := AComponent.Owner;
while Assigned( LOwner ) and not ( LOwner is T ) do
LOwner := LOwner.Owner;
Result := T( LOwner );
end;
class function TComponentUtil.GetParent<T>( AControl : TControl ) : T;
var
LParent : TWinControl;
begin
LParent := AControl.Parent;
while Assigned( LParent ) and not ( LParent is T ) do
LParent := LParent.Parent;
Result := T( LParent );
end;
end.

Sir Rufo
- 18,395
- 2
- 39
- 73
-
@DavidHeffernan ;o) the basic was up to you, i just tuned the painting :o) – Sir Rufo Jan 11 '13 at 21:14
-
Put them in a class helper and it can be even cleaner at the call site! – David Heffernan Jan 11 '13 at 21:35
1
As far the addition shown here was not asked, I'll add is an other answer
Function GetPageIndexOfControl(c:TControl;var PageControl:TPageControl):Integer;
begin
Result := -1;
While Assigned(c.Parent) and not (c is TTabsheet) do c := c.Parent;
if Assigned(c) then
if c is TTabsheet then
begin
Result := TTabsheet(c).PageIndex;
PageControl := TTabsheet(c).PageControl;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
pc:TPageControl;
i:Integer;
begin
I := GetPageIndexOfControl(Button1,PC);
if (i>-1) and Assigned(PC) then
begin
PC.ActivePageIndex := i;
if Assigned(PC.OnChange) then PC.OnChange(PC);
end;
end;

bummi
- 27,123
- 14
- 62
- 101