views:

218

answers:

1

I'm making a user control, and I manually draw the text string, but I can't seem to keep it centered. I need it to stay centered no matter what the font, or string length.

Thanks

A: 

If you're drawing the text using the System.Drawing.Graphics object's DrawString method, then you should use the same object's MeasureString method to get the pixel length of the string before drawing, and then offset the draw position accordingly (i.e. offset it by half the difference between the string's width and the width of the rectangle you're rendering it into.

The same principle applies if you're using the TextRenderer class.

This previous answer shows how to use these methods.

MusiGenesis