Solved

How to get text width for label

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

759 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

21 Experts available now in Live!

Get 1:1 Help Now