Solved

How to get text width for label

Posted on 2008-10-29
1
606 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

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.   …
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

810 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