473,417 Members | 1,566 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,417 software developers and data experts.

Get actual text width

Hi,
I want to calculate the actual text width. The word "init" is for example
smaller than the word "wimp". Is there an easy way to do this?

Thanks,
Mats-Lennart
Nov 16 '05 #1
6 39376
"Mats-Lennart Hansson" <ap********@hotmail.com> wrote in message
news:eZ**************@TK2MSFTNGP14.phx.gbl...
Hi,
I want to calculate the actual text width. The word "init" is for example
smaller than the word "wimp". Is there an easy way to do this?
Have a look at Graphics.MeasureString

Thanks,
Mats-Lennart

Nov 16 '05 #2
Mats,

Do you mean this

int strp = "wimp".Length;

I hope this helps?

Cor
Nov 16 '05 #3
wrong answer

Sorry

Cor
Nov 16 '05 #4
i think he want to measure a string width ;)
Nov 16 '05 #5
On Thu, 10 Mar 2005 13:08:27 +0200, Sean Hederman <us***@blogentry.com>
wrote:
"Mats-Lennart Hansson" <ap********@hotmail.com> wrote in message
news:eZ**************@TK2MSFTNGP14.phx.gbl...
Hi,
I want to calculate the actual text width. The word "init" is for
example
smaller than the word "wimp". Is there an easy way to do this?


Have a look at Graphics.MeasureString


MeasureString returns an approximation of the size of the string.
If you need exact measurement you need to use
Graphics.MeasureCharacterRanges
This code demonstrates the difference inside a paint event

Graphics g = e.Graphics;
string s1 = "init";
string s2 = "wimp";
string s3 = "initwimp";

StringFormat format = new StringFormat();
format.SetMeasurableCharacterRanges(new CharacterRange[]{new
CharacterRange(0, s1.Length)});
Region[] r = g.MeasureCharacterRanges(s1, this.Font, new Rectangle(0, 0,
1000, 1000), format);
RectangleF rect = r[0].GetBounds(g);

// using MeasureCharacterRanges
g.DrawString(s1, this.Font, SystemBrushes.ControlText, 0, 0);
g.DrawString(s2, this.Font, SystemBrushes.ControlText, rect.Width, 0);

// the assmbled string
g.DrawString(s3, this.Font, SystemBrushes.ControlText, 0, 20);

// using MeasureString
SizeF sf = g.MeasureString(s1, this.Font);
g.DrawString(s1, this.Font, SystemBrushes.ControlText, 0, 40);
g.DrawString(s2, this.Font, SystemBrushes.ControlText, sf.Width, 40);
--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #6
Thanks to both Morten and Sean. MeasureString solved my problems.

/Mats-Lennart

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:opsne7y8puklbvpo@pbn_computer...
On Thu, 10 Mar 2005 13:08:27 +0200, Sean Hederman <us***@blogentry.com>
wrote:
"Mats-Lennart Hansson" <ap********@hotmail.com> wrote in message
news:eZ**************@TK2MSFTNGP14.phx.gbl...
Hi,
I want to calculate the actual text width. The word "init" is for
example
smaller than the word "wimp". Is there an easy way to do this?


Have a look at Graphics.MeasureString


MeasureString returns an approximation of the size of the string.
If you need exact measurement you need to use
Graphics.MeasureCharacterRanges
This code demonstrates the difference inside a paint event

Graphics g = e.Graphics;
string s1 = "init";
string s2 = "wimp";
string s3 = "initwimp";

StringFormat format = new StringFormat();
format.SetMeasurableCharacterRanges(new CharacterRange[]{new
CharacterRange(0, s1.Length)});
Region[] r = g.MeasureCharacterRanges(s1, this.Font, new Rectangle(0, 0,
1000, 1000), format);
RectangleF rect = r[0].GetBounds(g);

// using MeasureCharacterRanges
g.DrawString(s1, this.Font, SystemBrushes.ControlText, 0, 0);
g.DrawString(s2, this.Font, SystemBrushes.ControlText, rect.Width, 0);

// the assmbled string
g.DrawString(s3, this.Font, SystemBrushes.ControlText, 0, 20);

// using MeasureString
SizeF sf = g.MeasureString(s1, this.Font);
g.DrawString(s1, this.Font, SystemBrushes.ControlText, 0, 40);
g.DrawString(s2, this.Font, SystemBrushes.ControlText, sf.Width, 40);
--
Happy Coding!
Morten Wennevik [C# MVP]

Nov 16 '05 #7

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

Similar topics

4
by: j.t.w | last post by:
Hi All. I'm having a problem with my Date of Birth textbox. When I open the ..htm file, the "DoB" textbox is flat with a border. All of my other textboxes are sunken and are yellow. When I...
8
by: BiNZGi | last post by:
Hi I have reduced the problem to this code: <form> <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tr> <td><input type="text" style="width: 100%;" value="Lorem ipsum dolor...
19
by: dmiller23462 | last post by:
Hi guys....I have absolutely NO IDEA what I'm doing with Javascript but my end result is I need two text boxes to stay hidden until a particular option is selected....I've cobbled together the...
2
by: george.leithead | last post by:
Hi all, I have a very strange problem! In following Web page (which is generated from a CMS System), the navigation to the left 'dissapears' when you roll the mouse over the links? It does not...
12
by: tim | last post by:
I am using foldoutmenu 3 and am having problems with viewing my menus in firefox. On my sub3 menus i have more than one line of text in some places. firefox does not recognise that there is more...
3
w33nie
by: w33nie | last post by:
I want to disable the text boxes, captain_name and captain_email, but only if the radio button, captain_guarantee, has NOT been checked. how do i do this? <form name="formTeamApplication"...
4
by: pablorp80 | last post by:
Hello, Here is what I need: I need the focus and the cursor set to a textbox named txtGT, every time no matter if it is the first page load or whether it is a postback. Here is the problem: I...
1
by: semomaniz | last post by:
I have a form where i have created the form dynamically. First i manually added a panel control to the web page. Then i added another panel dynamically and inside this panel i created tables. I have...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.