事件处理程序和鼠标单击 [英] Event handlers and mouse click

查看:86
本文介绍了事件处理程序和鼠标单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在弄清楚如何管理处理程序时遇到了麻烦。

问题是我有5个图片框,他们有共享处理程序,例如当我将鼠标悬停在他们上面时,他们会获得BorderStyle.Fixed3D if鼠标离开然后BorderStyle.None发生工作正常。但是这里有问题..我需要左键单击鼠标以保持图片框Stuck in BorderStyle.Fixed3D但我认为它不起作用因为MouseLeave事件有任何建议如何修复它?



我尝试过:



I'm having a trouble figuring out how to manage the handlers.
Problem is i have 5 picture box and they have shared handlers for example when I hover over them they get BorderStyle.Fixed3D if mouse leaves then BorderStyle.None occurs that works fine. But here is the problem.. I need to left click the mouse to keep the picturebox Stuck in BorderStyle.Fixed3D but I think it doesn't work because of the MouseLeave event any suggestion how to fix that ?

What I have tried:

private void SetEvents() //Initilize events
        {
            teningur1.MouseHover += PictureBoxes_MouseHover;
            teningur2.MouseHover += PictureBoxes_MouseHover;
            teningur3.MouseHover += PictureBoxes_MouseHover;
            teningur4.MouseHover += PictureBoxes_MouseHover;
            teningur5.MouseHover += PictureBoxes_MouseHover;

            teningur1.MouseLeave += PictureBoxes_MouseLeave;
            teningur2.MouseLeave += PictureBoxes_MouseLeave;
            teningur3.MouseLeave += PictureBoxes_MouseLeave;
            teningur4.MouseLeave += PictureBoxes_MouseLeave;
            teningur5.MouseLeave += PictureBoxes_MouseLeave;

            teningur1.MouseClick += PictureBoxes_MouseClick;
            teningur2.MouseClick += PictureBoxes_MouseClick;
            teningur3.MouseClick += PictureBoxes_MouseClick;
            teningur4.MouseClick += PictureBoxes_MouseClick;
            teningur5.MouseClick += PictureBoxes_MouseClick;
        }


        private void PictureBoxes_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ((PictureBox)sender).BorderStyle = BorderStyle.Fixed3D;
                
            }
            
        }


        //Event handlers
        static private void PictureBoxes_MouseHover(object sender, EventArgs e)
        {
            ((PictureBox)sender).BorderStyle = BorderStyle.Fixed3D;
        }

        static private void PictureBoxes_MouseLeave(object sender, EventArgs e)
        {
            ((PictureBox)sender).BorderStyle = BorderStyle.None;
        }

推荐答案

每个控件都有一个Tag属性:你可以用它来保存任何对象。

将所有图片框的Tag属性设置为BorderStyle.None

然后在你的悬停方法中:

Every control has a Tag property: you can use it to hold any object.
Set the Tag property of all your pictureboxes to BorderStyle.None
Then in your Hover method:
PictureBox p = sender as PictureBox;
if (p != null)
    {
    p.BorderStyle = BorderStyle.Fixed3D;
    }



在你的离职时:


In your Leave hander:

PictureBox p = sender as PictureBox;
if (p != null)
    {
    p.BorderStyle = (BorderStyle) p.Tag;
    }

和你的Click处理程序:

And your Click handler:

PictureBox p = sender as PictureBox;
if (p != null)
    {
    p.BorderStyle = BorderStyle.Fixed3D;
    p.Tag = p.BorderStyle;
    }


OriginalGriff的解决方案非常好,但也许这个更方便,更容易理解:



当你点击PictureBox时,由于MouseHover事件,边框已经设置为Fixed3D(顺便说一句,更好的是使用MouseEnter)。

所以如果你想要永久保留这种类型的边框,您只需取消订阅MouseLeave事件(您可以使用鼠标右键恢复边框机制):

OriginalGriff's solution is very good, but maybe this one's more convenient and easier to understand for you:

When you click on a PictureBox the border is already set to Fixed3D because of the MouseHover event (btw. better is to use MouseEnter).
So if you want to permanently keep this type of border you can just unsubscribe from the MouseLeave event (you can restore the border mechanism with the right mouse button):
private void PictureBoxes_MouseClick(object sender, MouseEventArgs e)
{
    PictureBox pBox = sender as PictureBox;

    if (e.Button == MouseButtons.Left)
    {
        pBox.MouseLeave -= PictureBoxes_MouseLeave;                
    }
    else if (e.Button == MouseButtons.Right)
    {
        pBox.MouseLeave -= PictureBoxes_MouseLeave; // prevent double subscription
        pBox.MouseLeave += PictureBoxes_MouseLeave;
    }
}


这篇关于事件处理程序和鼠标单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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