C# 如何循环表单中的所有按钮以更改它们在C中的文本#
我是C#新手,我使用windows窗体。我有C# 如何循环表单中的所有按钮以更改它们在C中的文本#,c#,C#,我是C#新手,我使用windows窗体。我有Form1,上面有20个按钮(button1到button20) 如何循环所有这20个按钮并将其文本更改为例如“Hello”文本 有人知道如何做到这一点吗?谢谢你在你表格的代码背后: foreach(var btnControl in this.Controls.OfType<Button>()) { btnControl.Text = "Hello"; } foreach(this.Controls.OfType()中的var
Form1
,上面有20个按钮(button1
到button20
)
如何循环所有这20个按钮
并将其文本更改为例如“Hello”文本
有人知道如何做到这一点吗?谢谢你在你表格的代码背后:
foreach(var btnControl in this.Controls.OfType<Button>())
{
btnControl.Text = "Hello";
}
foreach(this.Controls.OfType()中的var btnControl)
{
btnControl.Text=“你好”;
}
表单代码隐藏的某个地方:
foreach(var btnControl in this.Controls.OfType<Button>())
{
btnControl.Text = "Hello";
}
foreach(this.Controls.OfType()中的var btnControl)
{
btnControl.Text=“你好”;
}
在将容器(分组框、选项卡等)引入页面之前,一个简单的循环将一直有效。此时需要一个递归函数
private void ChangeButtons(Control.ControlCollection controls)
{
for (int i = 0; i < controls.Count; i++)
{
// The control is a container so we need to look at this control's
// children to see if there are any buttons inside of it.
if (controls[i].HasChildren)
{
ChangeButtons(controls[i].Controls);
}
// Make sure the control is a button and if so disable it.
if (controls[i] is Button)
{
((Button)controls[i]).Text = "Hello";
}
}
}
在将容器(GroupBox、选项卡等)引入页面之前,一个简单的循环将一直有效。此时需要一个递归函数
private void ChangeButtons(Control.ControlCollection controls)
{
for (int i = 0; i < controls.Count; i++)
{
// The control is a container so we need to look at this control's
// children to see if there are any buttons inside of it.
if (controls[i].HasChildren)
{
ChangeButtons(controls[i].Controls);
}
// Make sure the control is a button and if so disable it.
if (controls[i] is Button)
{
((Button)controls[i]).Text = "Hello";
}
}
}
查看右边距上的相关链接。==>查看右边距上的相关链接。==>ReSharper是VS的一个插件。我告诉我也要摆脱“这个”。但我是老派。哇,我把你的评论和“SharpDevelop”混在一起了。但是,是的,我想这只是偏好。ReSharper是VS-Mine的插件。我告诉我也要摆脱“这个”。但我是老派。哇,我把你的评论和“SharpDevelop”混在一起了。但我想这只是偏好。