On startup of this sample Windows forms application, the paint override uses DrawString to put a message on the screen. So far, so good. The problem is that there are three paint events on startup, and the 2nd or 3rd one causes the string to be drawn at a different location than where it is supposed to be.
I first noticed this problem in a more complicated application that I'm developing and created this simple example to eliminate any other code as the source of the problem. The problem also occurs with DrawRectangle, DrawLine, and DrawEllipse.
The 2nd and 3rd paint events occur 3 to 4 seconds after program startup. After that, the screen is redrawn correctly for the rest of program execution.
Is this possibly a bug in C# or am I missing something obvious? Here's the code. Thanks in advance.
public partial class Form1 : Form
protected override void OnPaint(PaintEventArgs eventArgs)
using (Font myFont = new System.Drawing.Font("Helvetica", 40, FontStyle.Italic))
eventArgs.Graphics.DrawString("Hello C#", myFont, System.Drawing.Brushes.Red, 200, 200);
} //myFont is automatically disposed here, even if an exception was thrown