tags:

views:

301

answers:

5

Hello,

I'm trying to draw a text on a panel(The panel has a background picture).

It works brilliant,but when I minimize and then maximize the application the text is gone.

My code:

using (Graphics gfx = Panel1.CreateGraphics())
{
    gfx.DrawString("a", new Font("Tahoma", 5), Brushes.White, new PointF(1, 1));
}

How do I keep it static so it doesn't get lost?

+4  A: 

Just add a handler for the Paint event:

private void panel1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawString("a", new Font("Tahoma", 5), Brushes.White, new PointF(1, 1));
}
Jon B
Well,I didn't say everything.I have 62 panels and I have to write different integer on each of them,is there another alternative or how would you suggest that to be done?
John
@John: I would suggest creating a control that inherits from Panel, and creating a property for the text. Just override OnPaint in your control to write the string (using the same code).
Jon B
Yes,but that means 60 OnPaint events - by hand. :(
John
@John: I think you misunderstand. You create a new control derived from Panel that implements the paint and then have a property specifying the number it should draw. Then you just use that new control in place of Panel wherever you need it and provide the property value.
Jeff Yates
A: 

When you draw something, it only remains until the next time the form is refreshed.

When the form is refreshed, the Paint event is called. So if you want to ensure your text doesn't disappear, you need to include the code that draws it in the Paint event.

You can trigger a repaint using Control.Invalidate, but you otherwise cannot predict when they will happen.

Jonathan Allen
The new problem is that I don't have to write "a" ,but a specific integer and the panel isn't one,is the paintEvent only way to accomplish this?
John
Make it a property of your class and simply draw the value of said property in OnPaint.
Ed Swangren
+7  A: 

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.

BFree
+1: Beat me to it by seconds :)
Reed Copsey
Just one question,should I call "TextToRender = "myText"" or it will get it by itself?
John
John: You'll need to set myPanel1.TextToRender = "a"; myPanel2.TextToRender = "b"; Other than that, they'll "just work"
Reed Copsey
Error "no suitable method found to override" on line 9 - the ovveride method.
John
You probably forgot to inherit from panel then.
Ed Swangren
+1  A: 

If you don't use a Paint event, you are just drawing on the screen where the control happens to be. The control is not aware of this, so it has no idea that you intended for the text to stay there...

If you put the value that you want drawn on the panel in it's Tag property, you can use the same paint event handler for all the panels.

Also, you need to dispose of the Font object properly, or you will be having a lot of them waiting to be finalized before they return their resources to the system.

private void panel1_Paint(object sender, PaintEventArgs e) {
   Control c = sender as Control;
   using (Font f = new Font("Tahoma", 5)) {
      e.Graphics.DrawString(c.Tag.ToString(), f, Brushes.White, new PointF(1, 1));
   }
}
Guffa
+1  A: 

Perhaps these links will help you:

http://www.bobpowell.net/creategraphics.htm

http://www.bobpowell.net

Chris Dunaway