Windows Develop Bookmark and Share   
 index > Windows Forms General > StackOverflowException when a simple OnPaint()
 

StackOverflowException when a simple OnPaint()

I have a very simple WinForm with the following code...When I run it I get stackOverflowException.
When I debug, I see that the OnPaint() method is called dozens of times. But I have nothing (no controls) on the form yet. This is a plain blank form...

namespace

 

SampleGraphics

{

 

public partial class MainForm : Form

{

 

public MainForm()

{

InitializeComponent();

}

 

private void OnPaint(object sender, PaintEventArgs e)

{

 

 

// Obtain a Graphics object from the incoming PaintEventArgs.

 

Graphics g = e.Graphics;

 

// Render a textual message in a given font and color.

g.DrawString(

"Hello GDI+", new Font("Times New Roman", 20), Brushes.Green, 0, 0);

 

// If overriding the Paint method, be sure to call the base class implementation.

 

base.OnPaint(e);

}

}

}

Ray Cacciatore  Tuesday, October 06, 2009 5:51 AM
You are not overriding the OnPaint event.  You are just hiding the inherited implementation with a new one.  Try declaring the OnPaint event like this:

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            //AFTER base.OnPaint(e), do your thing.  If you do it backwards, the base painting
            //will erase your custom painting.
        }


See if you continue to have problems.
MCP
webJose  Tuesday, October 06, 2009 5:58 AM
You are not overriding the OnPaint event.  You are just hiding the inherited implementation with a new one.  Try declaring the OnPaint event like this:

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            //AFTER base.OnPaint(e), do your thing.  If you do it backwards, the base painting
            //will erase your custom painting.
        }


See if you continue to have problems.
MCP
webJose  Tuesday, October 06, 2009 5:58 AM
Hello Ray,

Looks like you have confused the Form.OnPaint method with the Form.Paint event (inherited from Control).

If you want to use the Form.OnPaint method, then you need to override it like this:

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);

    Graphics g = e.Graphics;

    using (Font f = new Font("Times New Roman", 20))
    {
        g.DrawString("Hello GDI+", f, Brushes.Green, 0, 0);
    }
}


If you want to use the Paint event, then you don't call base.OnPaint in your event handler because the OnPaint method is responsible for raising the Paint event; what you are doing is to create an infinite loop of OnPaint & Paint calling each other and resulting the stack overflow.

private void MainForm_Paint(object sender, PaintEventArgs e)
{
    Graphics g = e.Graphics;

    using (Font f = new Font("Times New Roman", 20))
    {
        g.DrawString("Hello GDI+", f, Brushes.Green, 0, 0);
    }
}


Hope this makes it clear.

Regards,
Jie
MSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg[at]microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

If you have any feedback, please tell us.

The CodeFx Project
My Blog (in Simplified Chinese)
  • Edited byWang, JieMSFTTuesday, October 06, 2009 8:10 AMcode fix
  •  
Wang, Jie  Tuesday, October 06, 2009 8:09 AM

You can use google to search for other answers

Custom Search

More Threads

• Load dll on the spot
• How to display mdichild window over the controls hosted in mdiparent
• storing data to an array one by one.
• Print and Print Preview contents of Richtextbox (Using VS 2008 C#)
• Odd error that's keeping me from running my app:
• ComboBox referesh
• Display Live Icons in .NET apps
• Clear All TextBoxes in ToolStrip
• internet connection in/out
• Fonts or words