Ivan-Mark Debono
Ivan-Mark Debono

Reputation: 16330

Why doesn't a user control being redrawed?

I have a custom control and I'm drawing some of its contents like this:

public class TextItem
{
    public Font Font { get; set; }
}

    public TaskBox()
    {
        SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        SetStyle(ControlStyles.UserPaint, true);
        SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        SetStyle(ControlStyles.ResizeRedraw, true);

        this.items = new List<TextItem>();
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        base.OnMouseMove(e);

        foreach (TextItem item in items)
        {
            if (item.Bounds.Contains(e.Location))
            {
                item.ForeColor = Color.Red;
                Cursor.Current = Cursors.Hand;
            }
            else
            {
                item.ForeColor = Color.Black;
                Cursor.Current = Cursors.Default;
            }
        }
    }

The cursor does change accordingly, however the text is not changing color. Am I missing some initialization?

Upvotes: 0

Views: 52

Answers (1)

Hans Passant
Hans Passant

Reputation: 942138

You didn't tell it to redraw. Call this.Invalidate(); when you change any field or property that's also used in your OnPaint() method so the user can see the side-effect.

Also note that assigning Cursor.Current is iffy, that doesn't normally last long since the cursor shape is determined by the this.Cursor property. I suspect, but didn't check, that it ought to flicker. Assigning the property is better.

Upvotes: 2

Related Questions