如果在表单中我有10个面板按1到10的顺序命名,并且所有面板都注册了相同的事件myPanel_Click
private void myPanel_Click(object sender, EventArgs e)
{
}
我可以检索我在这10个面板中点击的面板的名称吗?
int panelClicked;
private void myPanel_Click(object sender, EventArgs e)
{
//not a single clue
}
答案 0 :(得分:5)
如果我理解正确,您应该能够将发件人转换为面板,然后取名称属性。
private void myPanel_Click(object sender, EventArgs e)
{
Panel target = sender as Panel;
if(target != null)
MessageBox.Show(target.Name);
}
答案 1 :(得分:0)
private void myPanel_Click(object sender, EventArgs e)
{
MessageBox.Show((Panel)sender.Name);
}
答案 2 :(得分:0)
您也可以Tag Property来引用您的面板,方法是将您的面板编号分配给相应的标签。
private void myPanel_Click(object sender, EventArgs e)
{
Panel p = (Panel)sender;
switch ((int)p.Tag )
{
case 1:
// Your Code for Panel 1
break;
case 2:
// Your Code for Panel 2
break;
// Your other Panels here
default:
break;
}
}