How to get text width for label
Posted on 2008-10-29
I need to find out the width, in pixels, of a text string when used in a Label control on a .NET windows form application. I know about the Graphics.MeasureString() method, but this requires that I have a Graphics instance, which as near as I can tell is only available in a Paint event. So my question is: How do I get the size in pixels of the text in a label control, without a Graphics instance? Alternatively, how do I get a Graphics instance if not in a Paint event handler? Is there another way altogether to get text width, given a text string and font?