开发者

Changing button properties in a loop, for example

开发者 https://www.devze.com 2023-03-06 05:12 出处:网络
I would like to know how it\'s possible to change button properties by a code w开发者_如何学编程hen we don\'t know a button name while writing it.

I would like to know how it's possible to change button properties by a code w开发者_如何学编程hen we don't know a button name while writing it. For example, I have a loop like this:

for (int i=0; i<5; ++i) {
 int buttonName = "button_" + i;
 buttonName.enabled = false;
}

Thanks in advance!


You can access the Controls collection of the parent containing the button like this:

if(parent.Controls.ContainsKey(buttonName))
{
  Button myButton = (Button)parent.Controls[buttonName];
  myButton.Enabled = false;
}

This will need a little extra work if your buttons are not contained within the same parent; ie. some buttons on a Form, some buttons on a Panel contained within that same form.

0

精彩评论

暂无评论...
验证码 换一张
取 消