C# winform 由id获取该控件!
3个回答
2013-05-26
展开全部
C#Control里没有ID这个属性只有Name属性 其实就是ID的意思 你可以根据Name属性找到这个控件 代码如下 private void button1_Click(object sender, EventArgs e)
{
string name = "label1";
Control control = null;
foreach (Control item in this.Controls)
{
control = GetControl(name, item);
if (control != null)
{
MessageBox.Show("类型是:" + control.GetType().ToString() + "\r\n" +
"坐标是:" + control.Location.ToString());
break;
}
}
if (control == null)
{
MessageBox.Show("没有找到“"+name+"”");
}
}
private Control GetControl(string name, Control parent)
{
foreach (Control Item in parent.Controls)
{
if (Item.Name == name)
{
return Item;
}
else
{
Control control = GetControl(name, Item);
if (control != null)
{
return control;
}
}
}
return null;
}
{
string name = "label1";
Control control = null;
foreach (Control item in this.Controls)
{
control = GetControl(name, item);
if (control != null)
{
MessageBox.Show("类型是:" + control.GetType().ToString() + "\r\n" +
"坐标是:" + control.Location.ToString());
break;
}
}
if (control == null)
{
MessageBox.Show("没有找到“"+name+"”");
}
}
private Control GetControl(string name, Control parent)
{
foreach (Control Item in parent.Controls)
{
if (Item.Name == name)
{
return Item;
}
else
{
Control control = GetControl(name, Item);
if (control != null)
{
return control;
}
}
}
return null;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询