1

I hope to access a TButton using tag. IS it possible?

for example, I hope set the caption of a TButton (button1 has tag 3) as 'aaa', I know I can use

button1.caption:='aaa';

but I hope to use tag '3' to access the tbutton and set the string value 'aaa'.

Welcome any comment

Thanks

interdev

arachide
  • 8,006
  • 18
  • 71
  • 134
  • You are going to have be a bit more explicit as to what it is you want to do. – Nick Hodges Dec 22 '10 at 14:41
  • @user262325 - will you have the tags unique or you want to set the caption for more than one component (have the same Tag for more buttons) ? –  Dec 22 '10 at 15:10

5 Answers5

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;
SimaWB
  • 9,246
  • 2
  • 41
  • 46
  • Maaan I always get late. I was giving the same answer. Also tags among the buttons must be unique, otherwise,you have the last button with the spesified tag. – ali_bahoo Dec 22 '10 at 14:56
  • sorry, taking back, what about Break; from the loop –  Dec 22 '10 at 14:58
  • @daemon: if user262325 has multiple buttons with the same tag? – SimaWB Dec 22 '10 at 15:02
2

There is no direct way to do

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

You can search through a form's components looking for something with a tag of 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'

But note that you could have plenty of components with the same tag, it's not guaranteed unique.

Larry Lustig
  • 49,320
  • 14
  • 110
  • 160
2

I think this should work:

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;
M Schenkel
  • 6,294
  • 12
  • 62
  • 107
1

Well, right now the Tag property is the same size as a Pointer, so you could, but you need to describe a bit more of what it is you'd like to do.

I'm not positive that this is going to continue to be the case moving into 64-bit Delphi, but I think that's the case too.

Edit: Yes, TComponent.Tag should be a NativeInt in future versions. References: Barry Kelly, Alexandru Ciobanu

Community
  • 1
  • 1
afrazier
  • 4,784
  • 2
  • 27
  • 30
  • In a conference in Brazil this year, David I. said that next year there will be a 64 bit and MAC compiler -- The Tag property will be a 64 big integer if code is compiled for 64 bit platform. –  Dec 22 '10 at 14:39
  • mac compiler? is it for apple's mac? – arachide Dec 22 '10 at 14:42
  • @user262325 - Yes, there's plans to make a Mac compiler for Delphi. See the [Roadmap](http://edn.embarcadero.com/article/39934) – afrazier Dec 22 '10 at 14:54
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;