DrawString calls inconsistant in Paint Handler!




I am using Graphics.DrawString in the paint handler of a label.
The text comes up and behaves as expected on all events such as resize of
the lable control.

The problem is when i move a messagebox/ another application( say notepad )
across the lable.

In this case a clean repaint of the text fails to appear. The text gets
drawn erratically or altogether disappears.

What could i be missing out on in this code.

I tried inheriting from System.Windows.Forms.Label and overriding the
onpaint method with same results. I had also tried

having the usercontrol as UserControlStyles.UserPaint.

I was trying to avoid having to create the Graphics object for the lable in
the main form as this is not advisable; but i

tried that also with even worse results.

Any help to get me have the paint handler working properly would be greatly


//code snippet
private void lblMessage_Paint(object sender, PaintEventArgs e)
if( lblMessage.Text != "" )
lblMessage.Text = "";
Graphics g = e.Graphics;// Graphics.FromHwnd( this.Handle );

//g.SmoothingMode = SmoothingMode.AntiAlias;

StringFormat sf = new StringFormat();
sf.Trimming = StringTrimming.EllipsisCharacter;
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
sf.FormatFlags = StringFormatFlags.LineLimit |
StringFormatFlags.FitBlackBox |

g.DrawString (
new RectangleF(
sf );


Bob Powell [MVP]

Why do you get the graphics, which may be a completely different graphics
object, when the correct Graphics object to draw on is handed to you in the
event arguments?

In fact, you're getting the paint notification from the label, handling that
and using the Graphics of the parent object. I'm not surprised it's feeling
a bit off-colour.

Just use the Graphics you're handed in the PaintEventArgs.

Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting

Find great Windows Forms articles in Windows Forms Tips and Tricks

Answer those GDI+ questions with the GDI+ FAQ

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

Andrew Smith \(Infragistics\)

You're using the VisibleClipBounds as the location where the text should be
drawn but that will be the area within the graphics object that can be
rendered to. So if only part of the window is invalidated, that value is
going to be just the area that needs to be rendered. You should render based
on the client area where you want to draw in the control and not based on
the VisibleClipBounds. For a test, try passing in your control's
clientrectangle if you want to fill the area of the control.


Thanks for the help Bob and Andrew,

It's an honour to get an answer from you guys;

Andrew's answer took me further;
But what fixed my problem was having the size of the rectangle being passed
to drawstring as that of the lable itself;

I was passing the Graphics.VisibleClipBounds and then Graphics.ClipBounds
as suggested by Andrew.

The fix was suggested to me by my friend Kiran Gurujane.

g.DrawString (strLblMessageText,
new RectangleF(
new PointF( 1,1 ),
new SizeF( lblMessage.Size.Width,lblMessage.Size.Height ) ),
sf );


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question