473,503 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Graphics.MeasureString too short

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 short string width.
What is it I am doing wrong?

Here the code I am using:
private void MinimizeDgvColumns(DataGridView dgv)
{
using (Graphics g = dgv.CreateGraphics())
{
foreach (DataGridViewColumn c in dgv.Columns)
{
string s = c.HeaderCell.Value.ToString();
c.Width = (int)graphics.MeasureString(s,
dgv.ColumnHeadersDefaultCellStyle.Font).Width;
}
}
}

Regards
Rainer Queck
Oct 9 '08 #1
6 10511

"Rainer Queck" wrote:
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 short string width.
What is it I am doing wrong?

Here the code I am using:
private void MinimizeDgvColumns(DataGridView dgv)
{
using (Graphics g = dgv.CreateGraphics())
{
foreach (DataGridViewColumn c in dgv.Columns)
{
string s = c.HeaderCell.Value.ToString();
c.Width = (int)graphics.MeasureString(s,
dgv.ColumnHeadersDefaultCellStyle.Font).Width;
}
}
}

Regards
Rainer Queck
Hi Rainer,

Graphics.MeasureString is a quick way to measure a string, but the resulting
size is only an approximate measure. Use Graphics.MeasureCharacterRanges()
or TextRenderer.MeasureText(). PS! I believe MeasureText pads the size by
default so use TextFormatFlags.NoPadding if this is an issue.
MeasureCharacterRanges is the most complex of the three measuring solutions,
but may give the best result.

--
Happy Coding!
Morten Wennevik [C# MVP]

Oct 9 '08 #2
Hello Morten,

thanks for your advices. I will give it a try.

Regards
Rainer

"Morten Wennevik [C# MVP]" <Mo************@hotmail.comschrieb im
Newsbeitrag news:28**********************************@microsof t.com...
>
"Rainer Queck" wrote:
>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 short string width.
What is it I am doing wrong?

Here the code I am using:
private void MinimizeDgvColumns(DataGridView dgv)
{
using (Graphics g = dgv.CreateGraphics())
{
foreach (DataGridViewColumn c in dgv.Columns)
{
string s = c.HeaderCell.Value.ToString();
c.Width = (int)graphics.MeasureString(s,
dgv.ColumnHeadersDefaultCellStyle.Font).Width;
}
}
}

Regards
Rainer Queck

Hi Rainer,

Graphics.MeasureString is a quick way to measure a string, but the
resulting
size is only an approximate measure. Use
Graphics.MeasureCharacterRanges()
or TextRenderer.MeasureText(). PS! I believe MeasureText pads the size by
default so use TextFormatFlags.NoPadding if this is an issue.
MeasureCharacterRanges is the most complex of the three measuring
solutions,
but may give the best result.

--
Happy Coding!
Morten Wennevik [C# MVP]

Oct 9 '08 #3
Hello Morten,

unfortunately you suggestions did not work. I still endup in having even
shorter "widths".
Currently I get the best result with Graphics.MeasureString and handing
over a string which is enlarged by "__" (two underscore).

This is a workaround and I would like to know how to do it right.

Regards
Rainer

"Morten Wennevik [C# MVP]" <Mo************@hotmail.comschrieb im
Newsbeitrag news:28**********************************@microsof t.com...
>
"Rainer Queck" wrote:
>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 short string width.
What is it I am doing wrong?

Here the code I am using:
private void MinimizeDgvColumns(DataGridView dgv)
{
using (Graphics g = dgv.CreateGraphics())
{
foreach (DataGridViewColumn c in dgv.Columns)
{
string s = c.HeaderCell.Value.ToString();
c.Width = (int)graphics.MeasureString(s,
dgv.ColumnHeadersDefaultCellStyle.Font).Width;
}
}
}

Regards
Rainer Queck

Hi Rainer,

Graphics.MeasureString is a quick way to measure a string, but the
resulting
size is only an approximate measure. Use
Graphics.MeasureCharacterRanges()
or TextRenderer.MeasureText(). PS! I believe MeasureText pads the size by
default so use TextFormatFlags.NoPadding if this is an issue.
MeasureCharacterRanges is the most complex of the three measuring
solutions,
but may give the best result.

--
Happy Coding!
Morten Wennevik [C# MVP]

Oct 9 '08 #4
Rainer Queck wrote:
Hello Morten,

unfortunately you suggestions did not work. I still endup in having
even shorter "widths".
Currently I get the best result with Graphics.MeasureString and
handing over a string which is enlarged by "__" (two underscore).

This is a workaround and I would like to know how to do it right.
You are setting the column width, but there is some margin so that the
columns don't run together, as a result the column content is allowed less
space than the column width. You'll have to add that margin to the content
width to get the required column width.
>
Regards
Rainer

"Morten Wennevik [C# MVP]" <Mo************@hotmail.comschrieb im
Newsbeitrag news:28**********************************@microsof t.com...
>>
"Rainer Queck" wrote:
>>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 short string width.
What is it I am doing wrong?

Here the code I am using:
private void MinimizeDgvColumns(DataGridView dgv)
{
using (Graphics g = dgv.CreateGraphics())
{
foreach (DataGridViewColumn c in dgv.Columns)
{
string s = c.HeaderCell.Value.ToString();
c.Width = (int)graphics.MeasureString(s,
dgv.ColumnHeadersDefaultCellStyle.Font).Width;
}
}
}

Regards
Rainer Queck

Hi Rainer,

Graphics.MeasureString is a quick way to measure a string, but the
resulting
size is only an approximate measure. Use
Graphics.MeasureCharacterRanges()
or TextRenderer.MeasureText(). PS! I believe MeasureText pads the
size by default so use TextFormatFlags.NoPadding if this is an issue.
MeasureCharacterRanges is the most complex of the three measuring
solutions,
but may give the best result.

--
Happy Coding!
Morten Wennevik [C# MVP]

Oct 9 '08 #5
"Rainer Queck" <Ra****@noemail.noemailwrote in
news:u9**************@TK2MSFTNGP04.phx.gbl:
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 short string width.
What is it I am doing wrong?

Here the code I am using:
private void MinimizeDgvColumns(DataGridView dgv)
{
using (Graphics g = dgv.CreateGraphics())
{
foreach (DataGridViewColumn c in dgv.Columns)
{
string s = c.HeaderCell.Value.ToString();
c.Width = (int)graphics.MeasureString(s,
dgv.ColumnHeadersDefaultCellStyle.Font).Width;
}
}
}
You need to have space for the gridlines to, and some margin.

This is how the text-box-columns calculate its preferred size:
protected internal override Size GetPreferredSize(Graphics g, object value)
{
Size extents = Size.Ceiling(g.MeasureString(GetText(value),
DataGridTableStyle.DataGrid.Font));
extents.Width += xMargin*2 + this.DataGridTableStyle.GridLineWidth;
extents.Height += yMargin;
return extents;
}
hint: The .NET source-code can be downloaded using this tool:
http://www.codeplex.com/NetMassDownloader

--
Rune Huseby
Oct 9 '08 #6
MeasureString has some usable overloads that may help you. For example, I use:

SizeF tabSize = graphics.MeasureString( "M", font, rectfText.Size, StringFormat.GenericTypographic );

This overload is described in http://msdn.microsoft.com/en-us/library/ms142109.aspx
The note under Remarks is of interest:

The MeasureString method is designed for use with individual strings and includes a small amount of extra space before and after the
string to allow for overhanging glyphs. Also, the DrawString method adjusts glyph points to optimize display quality and might
display a string narrower than reported by MeasureString. To obtain metrics suitable for adjacent strings in layout (for example,
when implementing formatted text), use the MeasureCharacterRanges method or one of the MeasureString methods that takes a
StringFormat, and pass GenericTypographic. Also, ensure the TextRenderingHint for the Graphics is AntiAlias.

Hope this helps,
Mike

"Rune Huseby" <ru**@pronto.tvwrote in message news:Xn**********************************@207.46.2 48.16...
"Rainer Queck" <Ra****@noemail.noemailwrote in
news:u9**************@TK2MSFTNGP04.phx.gbl:
>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 short string width.
What is it I am doing wrong?

Here the code I am using:
private void MinimizeDgvColumns(DataGridView dgv)
{
using (Graphics g = dgv.CreateGraphics())
{
foreach (DataGridViewColumn c in dgv.Columns)
{
string s = c.HeaderCell.Value.ToString();
c.Width = (int)graphics.MeasureString(s,
dgv.ColumnHeadersDefaultCellStyle.Font).Width;
}
}
}

You need to have space for the gridlines to, and some margin.

This is how the text-box-columns calculate its preferred size:
protected internal override Size GetPreferredSize(Graphics g, object value)
{
Size extents = Size.Ceiling(g.MeasureString(GetText(value),
DataGridTableStyle.DataGrid.Font));
extents.Width += xMargin*2 + this.DataGridTableStyle.GridLineWidth;
extents.Height += yMargin;
return extents;
}
hint: The .NET source-code can be downloaded using this tool:
http://www.codeplex.com/NetMassDownloader

--
Rune Huseby

Oct 10 '08 #7

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

Similar topics

1
9558
by: Paul Hoad | last post by:
I'm trying to use MeasureString() to determine the length in pixels of a string However to do I need a System.Drawing.Graphics object to do this I need to create a System.Drawing.Graphics...
1
4980
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...
0
2196
by: keith | last post by:
I did a test typing characters until fill out whole textbox (single line). Then I chcked width of this string in paint event e.Graphics.MeasureString(textBox1.Text,textBox1.Font).Width e.g....
0
1343
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...
4
2866
by: Anil Gupte | last post by:
I copied the following almost directly from http://msdn2.microsoft.com/en-us/library/6xe5hazb.aspx Dim instance As Graphics Dim text As String 'Dim strfont As Font Dim returnValue As...
1
8068
by: Nathan Laff | last post by:
I have a custom label, that as the width changes, the height will change to fit all the text in the label... I use to do this like this... int newHeight =...
9
5045
by: koschwitz | last post by:
Hi, I hope you guys can help me make this simple application work. I'm trying to create a form displaying 3 circles, which independently change colors 3 times after a random time period has...
6
2663
by: Dilip | last post by:
Howdy Folks I have a display where the Graphics.DrawString function is called to display something. Since the text seems to be larger than its bounding rectangle, the call basically splits the...
4
4856
nukefusion
by: nukefusion | last post by:
I'm having trouble using MeasureCharacterRanges. If I use it to get the width of the string "TEST" I would expect to get the same width as I would if I used it to get the individual widths of the...
0
7198
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
7072
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
7319
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...
1
6979
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
7449
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...
1
4998
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
3149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
373
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.