Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 例如,更改循环中的按钮属性_C#_Winforms - Fatal编程技术网

C# 例如,更改循环中的按钮属性

C# 例如,更改循环中的按钮属性,c#,winforms,C#,Winforms,我想知道,当我们在编写按钮时不知道按钮名称时,如何通过代码更改按钮属性。 例如,我有这样一个循环: for (int i=0; i<5; ++i) { int buttonName = "button_" + i; buttonName.enabled = false; } if(parent.Controls.ContainsKey(buttonName)) { Button myButton = (Button)parent.Controls[buttonName]; m

我想知道,当我们在编写按钮时不知道按钮名称时,如何通过代码更改按钮属性。 例如,我有这样一个循环:

for (int i=0; i<5; ++i) {
 int buttonName = "button_" + i;
 buttonName.enabled = false;
}
if(parent.Controls.ContainsKey(buttonName))
{
  Button myButton = (Button)parent.Controls[buttonName];
  myButton.Enabled = false;
}
for(int i=0;i您可以访问包含以下按钮的父级集合:

for (int i=0; i<5; ++i) {
 int buttonName = "button_" + i;
 buttonName.enabled = false;
}
if(parent.Controls.ContainsKey(buttonName))
{
  Button myButton = (Button)parent.Controls[buttonName];
  myButton.Enabled = false;
}

如果您的按钮不包含在同一个父级中,则需要做一些额外的工作;例如,表单上的一些按钮,同一表单中包含的面板上的一些按钮。

谢谢,它的工作方式如下
Button myButton=(Button)This.控件[buttonName]是的,当然,我忘了演员阵容,对不起。你也可以这样做。
Button myButton=this.Controls[buttonName]as Button;
。如果myButton在此之后为null,那么
this.Controls[buttonName]
不是一个按钮。