Solved

How to get text width for label

Posted on 2008-10-29
1
608 Views
Last Modified: 2013-12-17
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?
0
Comment
Question by:johneanderson
1 Comment
 
LVL 8

Accepted Solution

by:
dampsey earned 500 total points
ID: 22838553
the following code will do the work:

//this refers to a form instance.
            Graphics t = this.CreateGraphics();
            t.MeasureString("Hello World", new Font("Arial", 12));
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question