检测右键单击窗体上的每个PictureBox
我想在表格中的任何 PictureBox
上单击鼠标右键。我已经为一个 PictureBox
设置了右键单击功能。很好,但是我希望在表单上的 PictureBoxes
上的所有右键单击时触发一个事件。
I'm wanting to detect a right click on any PictureBox
in the form. I have already set up the right click function for one PictureBox
. This is fine but I would like one event that will fire for all right clicks on PictureBoxes
on the form.
此右键甚至需要知道 PictureBox
名称,因为上下文菜单对于某些图片框
。
This right click even will need to know the PictureBox
name because the context menu will be different for some of the PictureBoxes
.
这里是我为一个 PictureBox
的右键单击事件准备的代码。
Here is the code I have for the right click event for one PictureBox
.
private void DesktopIcon1Icon_MouseClick(object sender, MouseEventArgs e)
{
switch (e.Button)
{
case MouseButtons.Right:
{
DesktopIconRightclick.Show(this, new Point(e.X, e.Y));
}
break;
}
}
我需要修改此代码以在出现任何 PictureBox 。
I need to adapt this code to fire if any PictureBox
is right clicked.
示例更新
if (pic = DesktopIcon2)
{
openToolStripMenuItem.visible = false;
}
您可以使用一个事件将对 PictureBoxes
上的所有右键单击触发,如下所示:
You can use one event that will fire for all right clicks on PictureBoxes
like this:
public Form1()
{
InitializeComponent();
pictureBox1.MouseClick += pictureBox_MouseClick;
pictureBox2.MouseClick += pictureBox_MouseClick;
}
然后您可以使用发件人
查找 PictureBox
的名称
,例如:
Then you can use Sender
to find PictureBox
's Name
like this:
private void pictureBox_MouseClick(object sender, MouseEventArgs e)
{
var pic = (sender as PictureBox).Name;//pic is the Name of the PictureBox that is clicked
switch (e.Button)
{
case MouseButtons.Right:
{
MessageBox.Show(pic);//Just for example
DesktopIconRightclick.Show(this, new Point(e.X, e.Y));
}
break;
}
}