遍历form窗体上的所有Button,并可以得到其中任何一个
1个回答
展开全部
foreach (Control c in this.Controls)
{
//第一种情况:button在窗体上
Button btn = c as Button;
if (btn != null)
{
if (btn.Text == "确定")
{
//do sth.
}
}
//第二种情况:Button在panel等容器里面
Panel p = c as Panel;
if (p != null)
{
foreach (Control cc in p.Controls)
{
Button c_btn = c as Button;
if (c_btn != null)
{
if (c_btn.Text == "确定")
{
//do sth.
}
}
}
}
}
{
//第一种情况:button在窗体上
Button btn = c as Button;
if (btn != null)
{
if (btn.Text == "确定")
{
//do sth.
}
}
//第二种情况:Button在panel等容器里面
Panel p = c as Panel;
if (p != null)
{
foreach (Control cc in p.Controls)
{
Button c_btn = c as Button;
if (c_btn != null)
{
if (c_btn.Text == "确定")
{
//do sth.
}
}
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询