473,326 Members | 2,023 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,326 software developers and data experts.

MeasureString problem

I'm trying to create a function that will pad a string with spaces so
that the resulting string is a specific width (in this case 50points).
As expected, the following code when passed an empty string will add
a space and I see the width increase. All subsequent times through
the loop, the width does not increase though the spaces are being
added to "value"... Please help!

private string AdjustSpace(string text){
Form f = new Form();
Graphics g = f.CreateGraphics();
g.PageUnit = GraphicsUnit.Point;
float width;
SizeF size;
do{
size = g.MeasureString(text, new
Font(System.Drawing.FontFamily.GenericSerif,10));
width = size.Width;
text = " " + text;
}
while(width<50);

return text;
}
Nov 15 '05 #1
1 7304
Hi,
[inline]
"Jesse Wade" <jw***@rubytuesday.com> wrote in message
news:9d**************************@posting.google.c om...
I'm trying to create a function that will pad a string with spaces so
that the resulting string is a specific width (in this case 50points).
As expected, the following code when passed an empty string will add
If the text is empty, all spaces are trailing spaces. And MeasureString by
default doesn't count traling spaces. You need to create a StringFormat
object with the MeasureTrailingSpaces as a FormatFlag..
a space and I see the width increase. All subsequent times through
the loop, the width does not increase though the spaces are being
added to "value"... Please help!

private string AdjustSpace(string text) {
StringFormat sf = StringFormat.GenericDefault;
sf.FormatFlags|=StringFormatFlags.MeasureTrailingS paces;
Font fnt = new Font(System.Drawing.FontFamily.GenericSerif,10);
StringBuilder sb = new StringBuilder(text);
Form f = new Form();
Graphics g = f.CreateGraphics();
g.PageUnit = GraphicsUnit.Point;
// check if an extra space fits before actually inserting it
while (g.MeasureString( " "+sb.ToString(), fnt, 0, sf ).Width <=
50f)
{
sb.Insert( 0, ' ' );
}

fnt.Dispose();
g.Dispose();
frm.Dispose(); return sb.ToString(); }


Creating a Form each time is a bit of an overhead, maybe you can add a
Graphics parameter.

This will not give you perfect right alignment. If you do the drawing
yourself consider using:

StringFormat sf = StringFormat.GenericDefault;
sf.Alignment = StringAlignment.Far;
g.DrawString( text, myFont, myBrush, new RectangleF( x, y, x+50f, y), sf );
HTH,
Greetings
Nov 15 '05 #2

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

Similar topics

0
by: Alien | last post by:
Why does this code Console.WriteLine(g.MeasureString("a", new Font("Lucida Console", 11f)).Width); Console.WriteLine(g.MeasureString("aa", new Font("Lucida Console", 11f)).Width); produce...
1
by: Yaron | last post by:
Hi, I made a procedure which receives a Graphics parameter, and with that Graphics parameter, performs various things. The area of confusion here is that when I pass the procedure a Graphics...
2
by: Stuart Norris | last post by:
Dear Group, I have a question about the result from ‘graphics.MeasureString' when using a mono-spaced font. Firstly I assume that Courier New font is mono-spaced, ie each character takes up the...
8
by: Shimrit Ben-Yair | last post by:
Hi, I'm having problems with the measureString function. I am trying to measure the length of a string the user types into a text box. However, the size in pixels that this function returns is...
0
by: Meetali Goel | last post by:
Hi all, I can't determine why I keep getting "NotSupportedException" when g.MeasureString() is called in the following code (.NET CE). I have spent several hours but I cant seem to find the...
5
by: Marc | last post by:
I want to use Measurestring in a Class. In a form I write cInt(Me.CreateGraphics.Measurestring("Text", mFont).Width) What do I write in a class (I dont't have the Me.CreateGraphics there...) ...
7
by: teo | last post by:
Hallo, I'd like to retrieve the pixel length of a string. ------------------------ I decided to use MeasureString, but I have a problem with the graphic "instance" of it. I'm in a Sub and
2
by: teo | last post by:
I have to measure the length of a string in a Label, to set the adequate Label width (because not all browsers support the auto-size Label property). I decide to use the 'MeasureString'...
6
by: Rainer Queck | last post by:
Hello NG, I am trying to adjust the column width of a DataGridView to the Text in its column header. To do this I am using the Graphics.MeasureString method. Unfortunatly I alway get a too...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.