问题描述
我在弄清楚如何管理处理程序时遇到了麻烦。
问题是我有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;
}
推荐答案
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;
}
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;
}
}
这篇关于事件处理程序和鼠标单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!