?
Solved

How to get text width for label

Posted on 2008-10-29
1
Medium Priority
?
612 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 8

Accepted Solution

by:
dampsey earned 2000 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 Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

764 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