TPageControl得到索引

时间:2013-01-11 09:22:57

标签: windows delphi delphi-xe2 tpagecontrol

如何获取有对象的页面编号?例如:第一页上有一个Button1,第二页上有Button2,如何获得没有ActivePageIndex的Button1页面的编号?感谢。

5 个答案:

答案 0 :(得分:7)

想要找到特定类的最近父级是很常见的。因此,为实现这一目标而付出代价是值得的。

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

一旦你有了这个,你可以用它来解决你当前的问题。

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

将这些问题分开后,您可以在其他设置中使用GetParentWithClass

答案 1 :(得分: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;

答案 2 :(得分:2)

试试这个:

procedure TForm1.Button1Click(Sender: TObject);
begin
  if Button1.Parent is TTabSheet then
    ShowMessage(IntToStr(TTabSheet(Button1.Parent).PageIndex));
end;

如果按钮直接位于选项卡表中(没有面板或其他控件),则可以正常工作。如果您有面板,则可以走父链以查看父节点是否为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;

答案 3 :(得分:2)

我喜欢使用Generics来避免重复

PageIndex := TComponentUtil.GetParent<TTabSheet>( MyControl ).PageIndex;

有这样的课程

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.

答案 4 :(得分:1)

到目前为止,此处显示的添加内容未被询问,我将添加另一个答案

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;