-1

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 ...

Karnalta
  • 518
  • 1
  • 9
  • 24
  • I have got some clues, in fact the OnPaint event is not call when mouse enter, it's only call when mouse out. But it's strange, it seem it's another event that is called to draw the button when mouse in. But which one ? – Karnalta Aug 16 '10 at 13:30

1 Answers1

1

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

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536