如何獲取有對象的頁面的編號?例如:在第一頁上有一個Button1,在第二頁上有Button2,如何獲取沒有ActivePageIndex的Button1的頁面編號?謝謝。TPageControl獲取索引
1
A
回答
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
。
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
嘗試這種情況:
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;
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;
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.
+0
@DavidHeffernan; o)基本由你決定,我只是調整了畫:o) –
+0
把他們放在班級幫手,它可以更清潔的呼叫現場! –
相關問題
- 1. Linq獲取索引
- 2. 獲取行索引
- 3. ArrayList,獲取值,從索引()到索引()
- 4. 從索引獲取價值與索引
- 5. 如何手動獲取獲取(...)索引
- 6. RethinkDB:獲取索引內的索引,而不是索引名稱
- 7. CListCtrl獲取物品索引
- 8. SML - 獲取列表索引
- 9. 獲取Excel行索引
- 10. 獲取錨索引/散列
- 11. 如何獲取行索引?
- 12. 獲取ColumnListItem索引sapui5
- 13. 從IndexColorModel獲取索引?
- 14. Python - 獲取切片索引
- 15. 獲取hclust cutree的索引
- 16. Javascript/jQuery獲取iFrame索引
- 17. 獲取表項索引
- 18. 獲取GridView的行索引
- 19. Typescript索引獲取/設置
- 20. 獲取圖像索引
- 21. 獲取行索引jQuery中
- 22. 獲取NSArray索引號
- 23. 從Joptionpane獲取索引
- 24. AJAX獲取索引的URL
- 25. 獲取值的索引
- 26. 獲取數組索引
- 27. 獲取行的索引表
- 28. 按索引獲取PerformanceCounter
- 29. 獲取字符串索引
- 30. Python:獲取列表索引
+1不錯,通用 – bummi
謝謝,它的效果很好。 –