Fog Creek Software
Discussion Board




Question

Lets say i have an array of buttons: button1, button2, button3....button999.

thats what i need:

if edit1.text='something' then...
for i:=0 to 999 do begin

button1.caption:="something"+'odd';
button2.caption:="something";
....       
button999:='something+'odd';
else do "something"
end;

Where's the odd number of button name i need to change the caption of that button.
Is it esier way how to change such big button arrays captions???

for example illeagal function:

for i:=0 to 999 do begin
if (i div 2<>0) then button+i+.caption:="bla bla bla"; {i know its wrong}
end;

Kylixer
Thursday, October 17, 2002

My answer applies to Delphi, probably the same thing in Kylix.

The form has a Controls property, which holds references to every control on it.

All controls have a Tag property, which at design time you can set to a distinct value for all "odd" buttons.

Iterate over all the Controls using a for loop, when encountering a button (if Controls[I] is TButton), check its Tag property to see which caption to set.

Tag is a nice property to use for things like that.

I did not verify any of this, so my post may not be entirely accurate.

Big B
Thursday, October 17, 2002

Kylixer,

Here is an example procedure of something along the lines
of what you described, hope it helps.

This example handles only 6 buttons in a list, but it should
work with any number of buttons.


procedure TForm1.Button1Click(Sender: TObject);
var
ButtonList : TList;
i          : Integer;

begin
  ButtonList:=TList.Create;
  for i:=0 to 6 do begin  // change to whatever...
    ButtonList.Add(TButton.Create(Self));
    TButton(ButtonList.Items[i]).Parent:=Form1;
    TButton(ButtonList.Items[i]).Left:=i*100;
    TButton(ButtonList.Items[i]).Top:=50;
    TButton(ButtonList.Items[i]).Caption:='Foo';
  end;
  for i:=0 to ButtonList.Count-1 do begin
    if (i mod 2)=0 then
      TButton(ButtonList.Items[i]).Caption:='Even'
    else
      TButton(ButtonList.Items[i]).Caption:='Odd';
  end;
end;

Have fun,

Patrik
Thursday, October 17, 2002

You can make it work the quick, dirty and inefficient way by using FindComponent. This works:

TButton(FindComponent('Button' + IntToStr(i))).Caption := 'Button text';

Not sure what you are trying to achieve, but I would never dump 1000 buttons on a form. It is generally better to write your own component in such a situation.

Jan Derk
Thursday, October 17, 2002

There is a Pascal function, odd, which would be more readable than the mod 2 stuff.

Greg
Friday, October 18, 2002


999 buttons?

I *don't* want to see that thing.

Leonardo Herrera
Friday, October 18, 2002

*  Recent Topics

*  Fog Creek Home