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
Solved

How to get the width of a character in pixels? -- vb.net

Posted on 2004-10-11
5
544 Views
Last Modified: 2012-05-05
** vb.net **
I need to dynamically size my column header based on the text that is retrieved for the column header text.
If the column header is "XXXX" characters long, how do I convert this to its equivalent  pixels
Thanks
0
Comment
Question by:vbothello
5 Comments
 
LVL 28

Expert Comment

by:iboutchkine
ID: 12278631
try this

Imports System.Drawing.Graphics


 Dim g As Graphics = TextBox1.CreateGraphics()
  Dim size As SizeF = g.MeasureString(string1, TextBox1.Font)

Then size.Width will have the width in pixels of the string as it would be if
drawn in Textbox1


length in bytes
System.Text.Encoding.Default.GetByteCount

0
 

Author Comment

by:vbothello
ID: 12279645
that's nice but I am not using a textbox , its just a column header on a grid , for now I just calculated the length of characters on the column header text , and multiplied it by an arbitrary number to give me the pixels , I know its not the right way to do it but would love to know the right way to calculate the pixels.
0
 
LVL 48

Accepted Solution

by:
Mikal613 earned 250 total points
ID: 12282894
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

Suggested Solutions

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…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

839 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