检测右键单击窗体上的每个PictureBox [英] Detect Right Click On Every PictureBox On The Form

查看:81
本文介绍了检测右键单击窗体上的每个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;
    }
}

我需要修改此代码以在出现任何<右键单击code> 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;
    }
}

这篇关于检测右键单击窗体上的每个PictureBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆