Rob Smyth

Monday, 20 August 2007

Snippet - Sizing Text Controls Without OnPaint

We so often avoid considering font, screen resolution, etc when sizing a text control we often set a minimum size that looks good on our screen. For example, you need to display a value that may vary from say 1.1 to say 8,888,888.88 and need to set a width that will allow the value to be visible for all values.

Not hard, I just keep reinventing the wheel finding the how to. So having done this yet again today here is the tell all code snippet:
Graphics graphics = control.CreateGraphics();
SizeF size = graphics.MeasureString(text, font);
Proudly simple (Nigel - I did originally type 'embarrassingly simple', but you are having such an effect on me!).

No comments: