Solved

How to get text width for label

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now