473,811 Members | 1,788 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Graphics.Measur eString 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.Measur eString 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 MinimizeDgvColu mns(DataGridVie w dgv)
{
using (Graphics g = dgv.CreateGraph ics())
{
foreach (DataGridViewCo lumn c in dgv.Columns)
{
string s = c.HeaderCell.Va lue.ToString();
c.Width = (int)graphics.M easureString(s,
dgv.ColumnHeade rsDefaultCellSt yle.Font).Width ;
}
}
}

Regards
Rainer Queck
Oct 9 '08 #1
6 10567

"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.Measur eString 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 MinimizeDgvColu mns(DataGridVie w dgv)
{
using (Graphics g = dgv.CreateGraph ics())
{
foreach (DataGridViewCo lumn c in dgv.Columns)
{
string s = c.HeaderCell.Va lue.ToString();
c.Width = (int)graphics.M easureString(s,
dgv.ColumnHeade rsDefaultCellSt yle.Font).Width ;
}
}
}

Regards
Rainer Queck
Hi Rainer,

Graphics.Measur eString is a quick way to measure a string, but the resulting
size is only an approximate measure. Use Graphics.Measur eCharacterRange s()
or TextRenderer.Me asureText(). PS! I believe MeasureText pads the size by
default so use TextFormatFlags .NoPadding if this is an issue.
MeasureCharacte rRanges 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.comsch rieb im
Newsbeitrag news:28******** *************** ***********@mic rosoft.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.Measur eString 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 MinimizeDgvColu mns(DataGridVie w dgv)
{
using (Graphics g = dgv.CreateGraph ics())
{
foreach (DataGridViewCo lumn c in dgv.Columns)
{
string s = c.HeaderCell.Va lue.ToString();
c.Width = (int)graphics.M easureString(s,
dgv.ColumnHead ersDefaultCellS tyle.Font).Widt h;
}
}
}

Regards
Rainer Queck

Hi Rainer,

Graphics.Measur eString is a quick way to measure a string, but the
resulting
size is only an approximate measure. Use
Graphics.Measur eCharacterRange s()
or TextRenderer.Me asureText(). PS! I believe MeasureText pads the size by
default so use TextFormatFlags .NoPadding if this is an issue.
MeasureCharacte rRanges 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.Measur eString 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.comsch rieb im
Newsbeitrag news:28******** *************** ***********@mic rosoft.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.Measur eString 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 MinimizeDgvColu mns(DataGridVie w dgv)
{
using (Graphics g = dgv.CreateGraph ics())
{
foreach (DataGridViewCo lumn c in dgv.Columns)
{
string s = c.HeaderCell.Va lue.ToString();
c.Width = (int)graphics.M easureString(s,
dgv.ColumnHead ersDefaultCellS tyle.Font).Widt h;
}
}
}

Regards
Rainer Queck

Hi Rainer,

Graphics.Measur eString is a quick way to measure a string, but the
resulting
size is only an approximate measure. Use
Graphics.Measur eCharacterRange s()
or TextRenderer.Me asureText(). PS! I believe MeasureText pads the size by
default so use TextFormatFlags .NoPadding if this is an issue.
MeasureCharacte rRanges 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.Measur eString 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.comsch rieb im
Newsbeitrag news:28******** *************** ***********@mic rosoft.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.Measur eString method.
Unfortunatl y I alway get a too short string width.
What is it I am doing wrong?

Here the code I am using:
private void MinimizeDgvColu mns(DataGridVie w dgv)
{
using (Graphics g = dgv.CreateGraph ics())
{
foreach (DataGridViewCo lumn c in dgv.Columns)
{
string s = c.HeaderCell.Va lue.ToString();
c.Width = (int)graphics.M easureString(s,
dgv.ColumnHea dersDefaultCell Style.Font).Wid th;
}
}
}

Regards
Rainer Queck

Hi Rainer,

Graphics.Measu reString is a quick way to measure a string, but the
resulting
size is only an approximate measure. Use
Graphics.Measu reCharacterRang es()
or TextRenderer.Me asureText(). PS! I believe MeasureText pads the
size by default so use TextFormatFlags .NoPadding if this is an issue.
MeasureCharact erRanges 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******** ******@TK2MSFTN GP04.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.Measur eString 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 MinimizeDgvColu mns(DataGridVie w dgv)
{
using (Graphics g = dgv.CreateGraph ics())
{
foreach (DataGridViewCo lumn c in dgv.Columns)
{
string s = c.HeaderCell.Va lue.ToString();
c.Width = (int)graphics.M easureString(s,
dgv.ColumnHeade rsDefaultCellSt yle.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 GetPreferredSiz e(Graphics g, object value)
{
Size extents = Size.Ceiling(g. MeasureString(G etText(value),
DataGridTableSt yle.DataGrid.Fo nt));
extents.Width += xMargin*2 + this.DataGridTa bleStyle.GridLi neWidth;
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.Measur eString( "M", font, rectfText.Size, StringFormat.Ge nericTypographi c );

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 MeasureCharacte rRanges method or one of the MeasureString methods that takes a
StringFormat, and pass GenericTypograp hic. Also, ensure the TextRenderingHi nt for the Graphics is AntiAlias.

Hope this helps,
Mike

"Rune Huseby" <ru**@pronto.tv wrote in message news:Xn******** *************** ***********@207 .46.248.16...
"Rainer Queck" <Ra****@noemail .noemailwrote in
news:u9******** ******@TK2MSFTN GP04.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.Measur eString 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 MinimizeDgvColu mns(DataGridVie w dgv)
{
using (Graphics g = dgv.CreateGraph ics())
{
foreach (DataGridViewCo lumn c in dgv.Columns)
{
string s = c.HeaderCell.Va lue.ToString();
c.Width = (int)graphics.M easureString(s,
dgv.ColumnHead ersDefaultCellS tyle.Font).Widt h;
}
}
}

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 GetPreferredSiz e(Graphics g, object value)
{
Size extents = Size.Ceiling(g. MeasureString(G etText(value),
DataGridTableSt yle.DataGrid.Fo nt));
extents.Width += xMargin*2 + this.DataGridTa bleStyle.GridLi neWidth;
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
9581
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 object for which there is only two constructors System.Drawing.Graphics.FromHdc
1
5046
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 object created by a Panel, and use the MeasureString() method, I get a different width than when I pass up the procedure a Graphics object created by a PrintDocument. The procedure is exactly the same, independent of which object called it, so why...
0
2228
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. the textBox1.Width=268 I typed in 35 'A' with default Font. width retured from MeasureString function is 252
0
1373
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 solution. I would really appreciate it if someone could look at the code below. Label l = new Label();
4
2901
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 SizeF
1
8105
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 = (int)Math.Ceiling(g.MeasureString(this.Text, this.Font, this.Width - this.Padding.Left, StringFormat.GenericDefault).Height); Then override the onPaint and do this...
9
5067
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 passed. I'm struggling with making the delegate/invoke thing work, as I know GUI objects aren't thread-safe. I don't quite understand the concept I'm supposed to use to modify the GUI thread-safe. Below is my form and my Circle class. Currently,...
6
2685
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 string into multiple lines. Is there a way to find out into how many lines the string was split? I am asking because this string is being displayed inside an owner
4
4888
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 characters "T", "E", "S", and "T" and then added the resulting values together. The problem is that the two values don't seem to match unless the Graphics.PageUnit is set to Inch. I don't understand why it doesn't work with the default setting of...
0
9728
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10389
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10402
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10135
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9205
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7670
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5554
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4339
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.