2010-12-22 66 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; 
相关问题