Why does text drawn on a panel disappear?

Inherit from Panel, add a property that represents the text you need to write, and override the OnPaintMethod():

public class MyPanel : Panel
{
    public string TextToRender
    {
        get;
        set;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        e.Graphics.DrawString(this.TextToRender, new Font("Tahoma", 5), Brushes.White, new PointF(1, 1));
    }
}

This way, each Panel will know what it needs to render, and will know how to paint itself.

Leave a Comment