如何遍历Button按钮

liulangdeyuyu 2010-05-23 09:05:05
在一个窗体中有多个(数目不定)Button,name分别为:Button1、Button2、Button3.....

如何用for循环遍历button

for(int i=1;i<n;i++)
{
Button(i=1为Button1,i=2为Button2).visible=true;
}
...全文
628 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
liulangdeyuyu 2010-05-23
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 caozhy 的回复:]

for (int i = 0; i < n; i++)
if (this.Controls[string.Format("button{0}", i)] != null) this.Controls[string.Format("button{0}", i)].Visible = true;
[/Quote]

这个对我很有用
谢谢
liulangdeyuyu 2010-05-23
  • 打赏
  • 举报
回复
想到怎么做了,谢谢大家

foreach (Control c in this.Controls)
{
if (c.GetType() == typeof(Button))
{
for (int i = 1; i < n; i++)
{
if(c.Name=="button"+i)
c.visible = true;

}
}
}
xuchenggang 2010-05-23
  • 打赏
  • 举报
回复
关键是怎么把n前面的按钮设置属性
就是just4 2010-05-23
  • 打赏
  • 举报
回复
"name分别为:Button1、Button2、Button3"

asp.net遍历,直接this.Controls

js遍历,直接document.all
threenewbee 2010-05-23
  • 打赏
  • 举报
回复
for (int i = 0; i < n; i++)
if (this.Controls[string.Format("button{0}", i)] != null) this.Controls[string.Format("button{0}", i)].Visible = true;
jointan 2010-05-23
  • 打赏
  • 举报
回复
this.Controls[string.Format("button{0}", i)].Visible = true;
liulangdeyuyu 2010-05-23
  • 打赏
  • 举报
回复
n已经赋值了
liulangdeyuyu 2010-05-23
  • 打赏
  • 举报
回复
关键是怎么把n前面的按钮设置属性

threenewbee 2010-05-23
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 liulangdeyuyu 的回复:]
我主要是为了控制制定(用n指定)的按钮可见
[/Quote]
获取 Control.Name 属性,然后得到 n。
liulangdeyuyu 2010-05-23
  • 打赏
  • 举报
回复
我主要是为了控制制定(用n指定)的按钮可见
liulangdeyuyu 2010-05-23
  • 打赏
  • 举报
回复
n是可以赋值的
tristanhu 2010-05-23
  • 打赏
  • 举报
回复
   foreach (Control tmpControl in this.Controls)
{
if (tmpControl is Button)
{
tmpControl.Visible = false;
}
}
threenewbee 2010-05-23
  • 打赏
  • 举报
回复
void FindChild(Control Parent)
{
foreach (Control c in Parent)
{
if (c.GetType() == typeof(Button)) c.Visible = true;
if (c.Controls.Count > 0) FindChild(c);
}
}
修正下:
void FindChild(Control Parent)
{
foreach (Control c in Parent.Controls)
{
if (c.GetType() == typeof(Button)) c.Visible = true;
if (c.Controls.Count > 0) FindChild(c);
}
}
jointan 2010-05-23
  • 打赏
  • 举报
回复
foreach(Control ctl in this.Controls)
{
if(ctl is Button)
ctl.Visible=true;
}
wuyq11 2010-05-23
  • 打赏
  • 举报
回复
foreach(Control c in this.Controls)
{
if(c is Button){}
}
或Control[] controls = this.Controls;
threenewbee 2010-05-23
  • 打赏
  • 举报
回复
void FindTop()
{
foreach (Control c in this.Controls)
{
if (c.GetType() == typeof(Button)) c.Visible = true;
if (c.Controls.Count > 0) FindChild(c);
}
}

void FindChild(Control Parent)
{
foreach (Control c in Parent)
{
if (c.GetType() == typeof(Button)) c.Visible = true;
if (c.Controls.Count > 0) FindChild(c);
}
}

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧