473,398 Members | 2,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Width of String

Any ideas on how to get the width of a string in pixels?

Thanks,
Randy


Jun 13 '07 #1
4 1784
Randy,

If you have a Graphics instance for where you are going to render the
string to, you can call the MeasureString method on the Graphics instance,
passing the string and getting the size of the rendered text (in the
specified font) in return.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"randy1200" <ra*******@discussions.microsoft.comwrote in message
news:58**********************************@microsof t.com...
Any ideas on how to get the width of a string in pixels?

Thanks,
Randy


Jun 13 '07 #2
How do you get a Graphics instance of (say) a printer before you start
printing ?
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:#c*************@TK2MSFTNGP06.phx.gbl...
Randy,

If you have a Graphics instance for where you are going to render the
string to, you can call the MeasureString method on the Graphics instance,
passing the string and getting the size of the rendered text (in the
specified font) in return.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"randy1200" <ra*******@discussions.microsoft.comwrote in message
news:58**********************************@microsof t.com...
>Any ideas on how to get the width of a string in pixels?

Thanks,
Randy


Jun 13 '07 #3
On Wed, 13 Jun 2007 14:15:34 -0700, Ian Semmel <an****@rocketcomp.com.au>
wrote:
How do you get a Graphics instance of (say) a printer before you start
printing ?
That's a very good question, one for which I don't have a pure .NET
answer. As near as I can tell, .NET does not include this very useful
functionality.

You have a couple of other options:

* Refrain from doing any layout that requires measuring strings or
other things based on the Graphics until you get the first page. This
won't allow you to do any pagination feedback in your application until
the user has printed or previewed the document, but it is a fairly simple
approach.

* Use p/invoke to call CreateDC() with the specific printer driver you
want to use, and then use the resulting DC to create a Graphics instance.
This allows you to get a printer Graphics instance any time you want, but
may lead to having to do other non-.NET stuff as well (for example, I
don't see a way to get the printer driver filename in .NET, so you might
have to use p/invoke to do that as well).

Pete
Jun 13 '07 #4
http://www.codeproject.com/cs/media/measurestring.asp

HTH :)

randy1200 je napisao/la:
Any ideas on how to get the width of a string in pixels?

Thanks,
Randy
Jun 13 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Alex Shi | last post by:
In php, is there a way obtain the width of a charactor of a certain font? Alex -- ================================================== Cell Phone Batteries at 30-50%+ off retail prices!...
9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
24
by: Mike L | last post by:
This is for a Win form, in C# 2005. I want to load a datagrid, make some columns width 0, and then clean out the record I added. I get the error message, ""Index was out of range. Must be...
5
by: John Smith | last post by:
Here is my HTML table formatting code. <table border="1" width="100%"> <tr width = "100%"> <td width="33%">11asdhagdshaskgashjgahjgadhjgdjshdgasjdgajdgadjhgdgahjdgadhjsgad
2
by: Robson Carvalho Machado | last post by:
Dear friends, I'm dynamically creating a Hyperlink with spacer.gif as ImageURL that is an 1px transparent image only to determine link position, but as I create this link dynamically I could not...
2
by: strauchdieb | last post by:
hey everyone i try to figure out how to calculate string width in a textarea while typing in it. the textarea has a fixed start width. when the string gets longer as the textarea width, the...
1
by: Davo256000 | last post by:
hi there, i've recently started a student placement as a web developer and wondered if anyone could help me with the following problem: I'm using an ashx file to access messages that are stored in...
1
by: Durango2008 | last post by:
Hi, I am having a bit of trouble with my gridview table and the width of the columns. The gridview is made of Templates that contain LinkButton controls. The LinkButtons will display a string...
30
ADezii
by: ADezii | last post by:
This week’s Tip of the Week will clearly demonstrate how you can dynamically set the Drop Down List Width of a Combo Box to the length of the longest item in its Row Source. The inspiration for this...
3
by: =?Utf-8?B?QW5kcmV3?= | last post by:
Hello, friends, We use System.Windows.Forms.ComboBox in our c#.net 2005 app. We want to make the dropdown list wider than the comboBox's width. (Some items in the dropdown list have more...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.