why won't this MeasureString work?

G

Guest

I am trying to make a method that will autosize columns to fit the longest
text contained in a DataGrid.

My plan was to take the width of an arbitrary character (underscore, in this
case) and multiply this width to the longest # of characters found for each
column.

But it comes out wrong... I find the longest string no problem, but when I
multiply it by the width that I measured for a single character, it comes out
too big... the columns in the datagrid then get stretched out too wide,
instead of just snug like I wanted. What gives?

Font font = datagrid.Font;
float fontWidth = g.MeasureString("_", font).Width;

// find longest strings in each of datagrid's columns
....

// change column style's width to product of longest string length and font
width
styles[x].Width = (int)(max_widths[x] * fontWidth);
 
G

Guest

You cannot use the width of an arbitrary character becasue you don't have
fixed width characters. If you use a fixed width font, then your solution
will work, but with most of the fonts the letters are varying widths and so
you will not have a true length unless you sum the width of each character.
 
G

Guest

Err, sorry when I editted the post I must have deleted all mention of the
fact I use a monospace font- namely Courier New. Otherwise I wouldn't have
even tried it this way.

Using Courier New, I did a test and found it was getting the same font width
regardless of the character (like lowercase 'L' same width as underscore '_')
but this number seems to be too wide, because when I multiply this number by
the number of characters and apply it to my DataGrid, the columns end up too
wide

Les said:
You cannot use the width of an arbitrary character becasue you don't have
fixed width characters. If you use a fixed width font, then your solution
will work, but with most of the fonts the letters are varying widths and so
you will not have a true length unless you sum the width of each character.

MrNobody said:
I am trying to make a method that will autosize columns to fit the longest
text contained in a DataGrid.

My plan was to take the width of an arbitrary character (underscore, in this
case) and multiply this width to the longest # of characters found for each
column.

But it comes out wrong... I find the longest string no problem, but when I
multiply it by the width that I measured for a single character, it comes out
too big... the columns in the datagrid then get stretched out too wide,
instead of just snug like I wanted. What gives?

Font font = datagrid.Font;
float fontWidth = g.MeasureString("_", font).Width;

// find longest strings in each of datagrid's columns
...

// change column style's width to product of longest string length and font
width
styles[x].Width = (int)(max_widths[x] * fontWidth);
 
D

Doug Forster

Hi,

Read this:
http://windowsforms.net/articles/gdiptext.aspx

Cheers
Doug Forster

MrNobody said:
Err, sorry when I editted the post I must have deleted all mention of the
fact I use a monospace font- namely Courier New. Otherwise I wouldn't have
even tried it this way.

Using Courier New, I did a test and found it was getting the same font
width
regardless of the character (like lowercase 'L' same width as underscore
'_')
but this number seems to be too wide, because when I multiply this number
by
the number of characters and apply it to my DataGrid, the columns end up
too
wide

Les said:
You cannot use the width of an arbitrary character becasue you don't have
fixed width characters. If you use a fixed width font, then your
solution
will work, but with most of the fonts the letters are varying widths and
so
you will not have a true length unless you sum the width of each
character.

MrNobody said:
I am trying to make a method that will autosize columns to fit the
longest
text contained in a DataGrid.

My plan was to take the width of an arbitrary character (underscore, in
this
case) and multiply this width to the longest # of characters found for
each
column.

But it comes out wrong... I find the longest string no problem, but
when I
multiply it by the width that I measured for a single character, it
comes out
too big... the columns in the datagrid then get stretched out too
wide,
instead of just snug like I wanted. What gives?

Font font = datagrid.Font;
float fontWidth = g.MeasureString("_", font).Width;

// find longest strings in each of datagrid's columns
...

// change column style's width to product of longest string length and
font
width
styles[x].Width = (int)(max_widths[x] * fontWidth);
 
C

Christoph Nahr

I don't know for sure but MeasureString might do some padding to the
left and right of a string. Try creating a string of max_widths[x]
underscores, then measure that string.

How to create a string of duplicate characters:
string s = new string('_', max_widths);

If that's a little bit too small, remember that you may need to round
up the float result of MeasureString (Size.Ceiling or Math.Ceiling).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top