views:

15

answers:

1

Hi all,

I am making a CustomControl based on a ToolStripButton control, I am trying to know when the mouse is Hover the button to draw it differently. Here is a quick view of my code :

    private bool m_IsHover = false;        

    ...

    protected override void OnMouseEnter(EventArgs e)
    {
        m_IsHover = true;
        Debug.WriteLine("Mouse IN");
        base.OnMouseEnter(e);
    }

    protected override void OnMouseLeave(EventArgs e)
    {
        m_IsHover = false;
        Debug.WriteLine("Mouse OUT");
        base.OnMouseLeave(e);
    }

    ...

    protected override void OnPaint(PaintEventArgs e)
    {
        // Define rectangle used to draw
        Rectangle borderRec = new Rectangle(0, 0, this.Width - 1, this.Height - 1);

        if (m_IsHover)
        {
            // Draw border
            e.Graphics.DrawRectangle(m_BorderPen, borderRec);

            ...
        }
        else
        {
            // Default draw
            base.OnPaint(e);
        }
    }

My problem is that I clearly see in the debug panel that Mouse IN and Mouse OUT are right, so variable should be correctly set, but in the OnPaint event, I never enter in the m_IsHover conditionnal ...

I really don't understand what the problem is, it seem so easy ...

+1  A: 

The ToolStripItem.Select() method runs on MouseEnter. Call this.Invalidate() to force a repaint.

Hans Passant