如何确定ContextMenuStrip是在哪个控件上打开的?
程序中动态生成了若干个pictureBox,他们同为一个pictureBox[]的元素,并且他们的ContextMenuStrip指向同一个ContextMenuStrip实例,如何判断是哪个pictureBox元素打开的ContextMenuStrip呢?
------解决方案--------------------使用ContextMenuStrip.SourceControl 属性
------解决方案--------------------同楼上,就楼主的要求,可以这样
Control c = contextMenuStrip1.SourceControl;
if (c.GetType().Name == "PictureBox ")
{
MessageBox.Show(((PictureBox)c).Name);
}
------解决方案--------------------private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
ContextMenuStrip menu = (ContextMenuStrip)sender;
for (int i = 0; i < pictureBox.Count; i++)
{
if ((PictureBox)menu.SourceControl == pictureBox[i])
{
// 是pictureBox[i]
break;
}
}
}