469,133 Members | 1,443 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,133 developers. It's quick & easy.

Changing multiple button-properties in a for-loop ?

Dear all,

I've a lot of buttons named button1..button100 and want to change some of there propertys.
Maintaining the overview I'm looking for a possibility for something like:

System::Windows::Forms::Button * Wolfgang;

for (i=1;i<100;i++)
{
Wolfgang = ???;
Wolfgang->BackColor=Color::YellowGreen;
}

to avoid:

button1->BackColor = Color::YellowGreen;
button2->BackColor = Color::YellowGreen;
button3->BackColor = Color::YellowGreen;
....
But I don't know how to fill the gap ??? with code.

Many thanks for suggestions,

Wolfgang
Nov 17 '05 #1
5 1152
Hi,

One thing you can do is to iterate through all the parent's controls,
attempt to cast each control to a Button and if it is a button, change its
colour. You could put your 100 buttons on their own Panel to make this
easier - then all the Panel's Controls will be Buttons.

Steve

"Wolfgang" <wo**********@yahoo.de> wrote in message
news:OV**************@TK2MSFTNGP14.phx.gbl...
Dear all,

I've a lot of buttons named button1..button100 and want to change some of
there propertys.
Maintaining the overview I'm looking for a possibility for something like:

System::Windows::Forms::Button * Wolfgang;

for (i=1;i<100;i++)
{
Wolfgang = ???;
Wolfgang->BackColor=Color::YellowGreen;
}

to avoid:

button1->BackColor = Color::YellowGreen;
button2->BackColor = Color::YellowGreen;
button3->BackColor = Color::YellowGreen;
...
But I don't know how to fill the gap ??? with code.

Many thanks for suggestions,

Wolfgang

Nov 17 '05 #2
Hello Steve,

Yes, that was also my suggestion but it leads to something like:

counter = dynamic_cast<Button *>(Pad->Controls->Item[i]);

where I fetch the Button in the "counter"-variable.

Then I must decide if it's one of the Buttons I want to change (unfortunately you where right. I've

also Buttons A..Z which are not affected).

(O.k. I know I could use the tag-property for that)

and then change the property I want.

But for me it seems like searching for things I KNOW.

I know I want to change the Buttons named button1..100.

Hence I thought there would be a possibility to fetch

the button using it's name like button("button1");

Where I can change the string and get the according button.

But thanks for your suggestions.

Wolfgang

P.S.: If someone has a more elegant way than grouping the buttons

which belong together in a pannel to fetch them like described above

I'm still interested.

------------------------------------------------------------------------

"Steve McLellan" <sjm AT fixerlabs DOT com> schrieb im Newsbeitrag news:#o**************@TK2MSFTNGP12.phx.gbl...
Hi,

One thing you can do is to iterate through all the parent's controls,
attempt to cast each control to a Button and if it is a button, change its
colour. You could put your 100 buttons on their own Panel to make this
easier - then all the Panel's Controls will be Buttons.

Steve

"Wolfgang" <wo**********@yahoo.de> wrote in message
news:OV**************@TK2MSFTNGP14.phx.gbl...
Dear all,

I've a lot of buttons named button1..button100 and want to change some of
there propertys.
Maintaining the overview I'm looking for a possibility for something like:

System::Windows::Forms::Button * Wolfgang;

for (i=1;i<100;i++)
{
Wolfgang = ???;
Wolfgang->BackColor=Color::YellowGreen;
}

to avoid:

button1->BackColor = Color::YellowGreen;
button2->BackColor = Color::YellowGreen;
button3->BackColor = Color::YellowGreen;
...
But I don't know how to fill the gap ??? with code.

Many thanks for suggestions,

Wolfgang


Nov 17 '05 #3
Hi,

You might be able to do what you suggest you'd need to use reflection. I've
not got any experience doing that, unfortunately. You might be able to use a
regex to match the text on the buttons, if they have similar labels. You
might also want to think about redesigning things - having >100 controls on
a single parent control seems a bit unnecessary, esp if they're unconnected
to other things. You can easily use the interface designer to add them to an
invisible control and add that control to the current parent.

Hope someone can give you some more hope, good luck!

Steve

Yes, that was also my suggestion but it leads to something like:

counter = dynamic_cast<Button *>(Pad->Controls->Item[i]);

where I fetch the Button in the "counter"-variable.

Then I must decide if it's one of the Buttons I want to change
(unfortunately you where right. I've

also Buttons A..Z which are not affected).

(O.k. I know I could use the tag-property for that)

and then change the property I want.

But for me it seems like searching for things I KNOW.

I know I want to change the Buttons named button1..100.

Hence I thought there would be a possibility to fetch

the button using it's name like button("button1");

Where I can change the string and get the according button.

But thanks for your suggestions.

Wolfgang

P.S.: If someone has a more elegant way than grouping the buttons

which belong together in a pannel to fetch them like described above

I'm still interested.

------------------------------------------------------------------------

"Steve McLellan" <sjm AT fixerlabs DOT com> schrieb im Newsbeitrag
news:#o**************@TK2MSFTNGP12.phx.gbl...
Hi,

One thing you can do is to iterate through all the parent's controls,
attempt to cast each control to a Button and if it is a button, change
its
colour. You could put your 100 buttons on their own Panel to make this
easier - then all the Panel's Controls will be Buttons.

Steve

"Wolfgang" <wo**********@yahoo.de> wrote in message
news:OV**************@TK2MSFTNGP14.phx.gbl...
> Dear all,
>
> I've a lot of buttons named button1..button100 and want to change some
> of
> there propertys.
> Maintaining the overview I'm looking for a possibility for something
> like:
>
> System::Windows::Forms::Button * Wolfgang;
>
> for (i=1;i<100;i++)
> {
> Wolfgang = ???;
> Wolfgang->BackColor=Color::YellowGreen;
> }
>
> to avoid:
>
> button1->BackColor = Color::YellowGreen;
> button2->BackColor = Color::YellowGreen;
> button3->BackColor = Color::YellowGreen;
> ...
>
>
> But I don't know how to fill the gap ??? with code.
>
> Many thanks for suggestions,
>
> Wolfgang
>
>


Nov 17 '05 #4
On Wed, 17 Nov 2004 17:10:58 +0100, "Wolfgang" <wo**********@yahoo.de>
wrote:
Dear all,

I've a lot of buttons named button1..button100 and want to change some of there propertys.
Maintaining the overview I'm looking for a possibility for something like:

System::Windows::Forms::Button * Wolfgang;

for (i=1;i<100;i++)
{
Wolfgang = ???;
Wolfgang->BackColor=Color::YellowGreen;
}

to avoid:

button1->BackColor = Color::YellowGreen;
button2->BackColor = Color::YellowGreen;
button3->BackColor = Color::YellowGreen;
...
But I don't know how to fill the gap ??? with code.


Put the buttons in an array (perhaps when you create them), and
iterate over that array.

Tom
Nov 17 '05 #5
That sounds interesting,
Thanks for that idea - I'll have a try.

Wolfgang

Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by michael nieuwenhuizen | last post: by
6 posts views Thread by Adam C. | last post: by
14 posts views Thread by Jeff | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.