I have a Panel
that contains child controls.
If I handle the Panel
\'s MouseEnter
and MouseLeave
events, and its c
If you dont mind creating a usercontrol(derived from the panel or other parent container you wish),
Override your parent's OnMouseLeave
method to look like the following..
protected override void OnMouseLeave(EventArgs e)
{
if(this.ClientRectangle.Contains(this.PointToClient(Control.MousePosition)))
return;
else
{
base.OnMouseLeave(e);
}
}
Then, the event raising will be in the required order.
This may not be the most elegant solution, but you could set a property in the parent control panel (subclass panel) that is a bool value like "bool selected". Then when the MouseEnter for the panel fires set it to true...then stop the mouseleave logic from firing unless it is set to false.
example
bool selected;
MouseEnter(..,..)
{
if (!selected)
selected = true;
else
selected = false;
if (selected)
/.. Logic Here ../
}
MouseLeave()
{
if (selected)
return;
/.. Logic Here ../
}
In reality I would just have the MouseLeave event of the child set the parameter.
Example:
Parent:
bool doLeave;
MouseLeave(..,..)
{
if (doLeave)
{
/.. Logic ../
doLeave = false;
}
Child:
MouseLeave(..., ...)
{
DerivedPanel parent = this.Parent as DerivedPanel;
if (parent != null)
parent.doLeave = true;
}
Neither are elegant but it will work.
Jimmy T. is right. There will be problems if there is no (or small) space betwean Parent Control (Panel) edge and Child Control.
This is how I solve this problem in UserControl-derived class:
public CSStackPanelItem()
{
InitializeComponent();
MouseEnter += new EventHandler(CSStackPanelItem_MouseEnter);
foreach (Control child in Controls)
{
child.MouseEnter += (s, e) => CSStackPanelItem_MouseEnter(s, e);
child.MouseLeave += (s, e) => OnMouseLeave(e);
}
}
protected override void OnMouseLeave(EventArgs e)
{
if (this.ClientRectangle.Contains(this.PointToClient(Control.MousePosition)))
return; //suppress mouse leave event handling
if (m_bIsHover)
{
m_bIsHover = false;
Invalidate(); //actually my mouse Enter/Leave event
}
base.OnMouseLeave(e);
}
void CSStackPanelItem_MouseEnter(object sender, EventArgs e)
{
m_bIsHover = true;
Invalidate(); //actually my mouse Enter/Leave event
}
Matthew's answer will not work always. Especially if the child control is set to the edge of its container and the mouse moves off the controls in that direction. You will never detect the MouseLeave event.
The best approach is to create a user control container then hook all the child controls' MouseEnter and MouseLeave events so that you can properly detect when and where the mouse is at all times. THEN if it enters your container's bounds you can fire a custom MouseEnter event and when it leaves MouseLeave event.
I believe so. A nice tool to have for verifying your WinForms application's events.
http://missico.spaces.live.com/blog/cns!7178D2C79BA0A7E3!186.entry
Check the child component..
if (panel1.GetChildAtPoint(panel1.PointToClient(Cursor.Position)) == null)
{
// mouse leave panel and children
}