Microsoft KB Archive/316572

= BUG: ListView.ColumnHeaderCollection.Add Method Does Not Work with Autosize Width in C# =

Article ID: 316572

Article Last Modified on 2/22/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-



This article was previously published under Q316572



SYMPTOMS
When you pass -1 or -2 autosize width settings to the ListView.ColumnHeaderCollection.Add method, the ListView control does not display the column as expected.

You expect a -1 setting to set the column header to the size of the largest subitem text in the column, and a -2 setting to set the column header to the size of the text in the column header.



RESOLUTION
To work around this issue, avoid using the -1 and -2 autosize parameters. Instead, pass a predefined column width to the Add method.

The other available workaround is more involved. The following steps demonstrate how to implement the -1 setting by setting the column width after the column has been created, and how to implement the -2 setting by using GDI+ to measure the string size of the column header text and the subitem text (in pixels).

To do this, follow these steps:  Use C# to start a new Windows application. Add a ListView control and two Button controls on Form1. These controls will be named listView1, button1, and button2, respectively.  In the Form1_Load event, add the following code: listView1.Bounds = new Rectangle(new Point(10,10), new Size(250,100)); listView1.View = View.Details; listView1.Items.Add(&quot;This is Item 1&quot;); listView1.Items[0].SubItems.Add(&quot;This is subitem 1 for item 1&quot;); listView1.Items[0].SubItems.Add(&quot;This is subitem 2 for item 1&quot;); listView1.Items.Add(&quot;This is Item 2&quot;); listView1.Items[1].SubItems.Add(&quot;This is subitem 1 for item 2&quot;); listView1.Items[1].SubItems.Add(&quot;This is subitem 2 for item 2&quot;); listView1.Columns.Add(&quot;Column 1&quot;, -1, HorizontalAlignment.Left); listView1.Columns.Add(&quot;Column 2&quot;, -2, HorizontalAlignment.Left); listView1.Columns.Add(&quot;Column 3&quot;, -1, HorizontalAlignment.Left);   In the button1_Click event, add the following code: //To implement the -1 autosize setting, set the width of columns after the columns are created, as follows: listView1.Columns[0].Width = -1; listView1.Columns[1].Width = -1; listView1.Columns[2].Width = -1;   In the button2_Click event, add the following code: //Use GDI+ to implement the -2 autosize setting. listView1.Clear; string s1 = &quot;Column 1&quot;; string s2 = &quot;Column 2&quot;; string s3 = &quot;column 3&quot;; Graphics newGraphics = Graphics.FromHwnd(listView1.Handle); SizeF stringSize1 = new SizeF; SizeF stringSize2 = new SizeF; SizeF stringSize3 = new SizeF; stringSize1 = newGraphics.MeasureString(s1, listView1.Font); stringSize2 = newGraphics.MeasureString(s2, listView1.Font); stringSize3 = newGraphics.MeasureString(s3, listView1.Font); listView1.View = View.Details; listView1.Items.Add(&quot;This is Item 1&quot;); listView1.Items[0].SubItems.Add(&quot;This is subitem 1 for item 1&quot;); listView1.Items[0].SubItems.Add(&quot;This is subitem 2 for item 1&quot;); listView1.Items.Add(&quot;This is Item 2&quot;); listView1.Items[1].SubItems.Add(&quot;This is subitem 1 for item 2&quot;); listView1.Items[1].SubItems.Add(&quot;This is subitem 2 for item 2&quot;); listView1.Columns.Add(&quot;Column 1&quot;, Convert.ToInt16(stringSize1.Width)+ 5, HorizontalAlignment.Left); listView1.Columns.Add(&quot;Column 2&quot;, Convert.ToInt16(stringSize2.Width)+ 5, HorizontalAlignment.Left); listView1.Columns.Add(&quot;Column 3&quot;, Convert.ToInt16(stringSize3.Width)+ 5, HorizontalAlignment.Left);  Press F5 to run the code. Click button1, and then notice that the column width is adjusted according to the size of the largest subitem text in the column. Click button2, and then notice that the column width is adjusted according to the size of the column header text.</ol>

<div class="status_section">

STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

<div class="moreinformation_section">

Steps to Reproduce the Problem
<ol> Use C# .NET to start a new Windows application.</li> On Form1, add a ListView control.</li>  In the Form1_Load event, add the following code: listView1.View = View.Details; listView1.Items.Add(&quot;This is Item 1&quot;); listView1.Items[0].SubItems.Add(&quot;This is subitem 1&quot;); listView1.Items[0].SubItems.Add(&quot;This is subitem 2&quot;); listView1.Items.Add(&quot;This is Item 2&quot;); listView1.Items[1].SubItems.Add(&quot;This is subitem 3&quot;); listView1.Items[1].SubItems.Add(&quot;This is subitem 4&quot;); listView1.Columns.Add(&quot;Column 1&quot;, -1, HorizontalAlignment.Left); listView1.Columns.Add(&quot;Column 2&quot;, -2, HorizontalAlignment.Left); listView1.Columns.Add(&quot;Column 3&quot;, -1, HorizontalAlignment.Left); </li> Press F5 to run the application.

Notice that the columns are not displayed at all.</li> Change the -2 and -1 settings to a fixed column width -- for example, 60 .</li> Run the application again. The columns are now displayed.</li></ol>

Keywords: kblistview kbctrl kbbug kbpending KB316572

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.