2010-12-22 36 views
1

我希望能夠使用標籤來訪問TButton。 有可能嗎?是否可以使用標籤訪問TButton?

例如,希望設置一個TButton(button1的具有標籤3)爲 'AAA', 我知道的標題我可以使用

button1.caption:= 'AAA';

但我希望使用標籤'3'來訪問tbutton並設置字符串值'aaa'。

歡迎任何評論

感謝

InterDev中

+0

你將會有更多一點明確的,什麼是你想做的事情。 – 2010-12-22 14:41:28

+0

@ user262325 - 您是否擁有唯一標籤或您想爲多個組件設置標題(對於更多按鈕,標籤是否相同)? – 2010-12-22 15:10:42

回答

5
procedure TForm1.ChnCaptionByTag(SearchTag: integer; NewCpt: string); 
var 
    i: Integer; 
begin 
    for i := 0 to ComponentCount - 1 do 
    if Components[i] is TButton then 
    begin 
     if TButton(Components[i]).Tag = SearchTag then 
     TButton(Components[i]).Caption := NewCpt; 
    end; 
end; 
1

那麼現在Tag屬性是大小爲指針一樣,所以你,但你需要描述多一點你想要做什麼。

我不確定這種情況會繼續發展到64位Delphi,但我認爲也是如此。

編輯:是的,在未來的版本中,TComponent.Tag應該是NativeInt。參考文獻:Barry KellyAlexandru Ciobanu

+0

在今年巴西的一次會議上,David I.說明年將會有一個64位和MAC編譯器 - 如果代碼是爲64位平臺編譯的話,Tag屬性將是一個64位的大整數。 – ComputerSaysNo 2010-12-22 14:39:39

+0

mac編譯器?是蘋果的mac嗎? – arachide 2010-12-22 14:42:47

+0

@Dorin - 謝謝,我發現了一些參考文獻... – afrazier 2010-12-22 14:45:02

2

有做

ButtonByTag(3).Caption := 'aaa'; 

您可以通過表單的組件搜索與3標籤尋找的東西沒有直接的方法:

var C: TComponent; 

for C in Self.Components do 
    if C is TCustomButton then 
     if C.Tag = 3 then 
     (C as TCustomButton).Caption := 'aaa' 

但請注意,您可能有大量具有相同標記的組件,但並不保證它是唯一的。

2

我認爲這應該工作:

procedure TForm1.SetCaption(iTag: Integer; mCaption: String); 
var 
    i: Integer; 
begin 
    for i:= 0 to controlcount-1 do 
    if controls[i] is TButton then 
     if TButton(controls[i]).Tag = iTag then 
     TButton(controls[i]).Caption := mCaption; 
end; 

procedure TForm1.Button2Click(Sender: TObject); 
begin 
    SetCaption(3,'aaa'); 
end; 
0
procedure TForm1.ChangeCaptionByTag(const SearchTag: integer; const NewCaption: string); 
var i: Integer; 
begin 
    for i in Components do 
    if Components[i] is TButton then 
     if (Components[i] as TButton).Tag = SearchTag then 
     begin 
      (Components[i] as TButton).Caption := NewCaption; 
      Break; 
     end; 
end; 
相關問題