Microsoft KB Archive/317546

= BUG: ListView.ColumnHeaderCollection.Add method does not work with autosize width in Visual Basic .NET =

Article ID: 317546

Article Last Modified on 2/22/2007

-

APPLIES TO


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

-



This article was previously published under Q317546



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 sub-item 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 sub-item text (in pixels).

To do this, follow these steps:  Use Microsoft Visual Basic .NET 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&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;, -1, 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. Dim newGraphics As Graphics = Graphics.FromHwnd(ListView1.Handle) Dim s1 As String = &quot;Column 1&quot; Dim s2 As String = &quot;Column 2&quot; Dim s3 As String = &quot;Column 3&quot; Dim StringSize1 As SizeF = newGraphics.MeasureString(s1, ListView1.Font) Dim StringSize2 As SizeF = newGraphics.MeasureString(s2, ListView1.Font) Dim StringSize3 As SizeF = newGraphics.MeasureString(s3, ListView1.Font) ListView1.Clear 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(s1, Convert.ToInt16(StringSize1.Width + 5), HorizontalAlignment.Left) ListView1.Columns.Add(s2, Convert.ToInt16(StringSize2.Width + 5), HorizontalAlignment.Left) ListView1.Columns.Add(s3, 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 the size of the largest sub-item 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 Visual Basic .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: kbvs2002sp1sweep kblistview kbctrl kbbug kbpending KB317546

-

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

© Microsoft Corporation. All rights reserved.