BUG: ListView.ColumnHeaderCollection.Add method does not work with autosize width in Visual Basic .NET (317546)
The information in this article applies to:
- Microsoft .NET Framework 1.1
- Microsoft .NET Framework 1.0
- Microsoft Visual Basic .NET (2003)
- Microsoft Visual Basic .NET (2002)
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("This is Item 1")
ListView1.Items(0).SubItems.Add("This is subitem 1")
ListView1.Items(0).SubItems.Add("This is subitem 2")
ListView1.Items.Add("This is Item 2")
ListView1.Items(1).SubItems.Add("This is subitem 3")
ListView1.Items(1).SubItems.Add("This is subitem 4")
ListView1.Columns.Add("Column 1", -1, HorizontalAlignment.Left)
ListView1.Columns.Add("Column 2", -1, HorizontalAlignment.Left)
ListView1.Columns.Add("Column 3", -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 = "Column 1"
Dim s2 As String = "Column 2"
Dim s3 As String = "Column 3"
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("This is Item 1")
ListView1.Items(0).SubItems.Add("This is subitem 1")
ListView1.Items(0).SubItems.Add("This is subitem 2")
ListView1.Items.Add("This is Item 2")
ListView1.Items(1).SubItems.Add("This is subitem 3")
ListView1.Items(1).SubItems.Add("This is subitem 4")
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.
STATUSMicrosoft has confirmed that this is a bug in the Microsoft
products that are listed at the beginning of this article.
Modification Type: | Minor | Last Reviewed: | 9/14/2005 |
---|
Keywords: | kbvs2002sp1sweep kbListView kbCtrl kbbug kbpending KB317546 |
---|
|