ListView Control

G

Guest

I have a form which has a ListView control named ListView1 added at design
time. When I add items using the following code, they don't appear in the
list view. However, if I create a ListView control in code and add it to the
form, it works. Why don't the items show up in the ListView that I added at
desgn time.

' Create three items and three sets of subitems for each item.
Dim item1 As New ListViewItem("item1", 0)
' Place a check mark next to the item.
item1.Checked = True
item1.SubItems.Add("1")
item1.SubItems.Add("2")
item1.SubItems.Add("3")
Dim item2 As New ListViewItem("item2", 1)
item2.SubItems.Add("4")
item2.SubItems.Add("5")
item2.SubItems.Add("6")
Dim item3 As New ListViewItem("item3", 0)
' Place a check mark next to the item.
item3.Checked = True
item3.SubItems.Add("7")
item3.SubItems.Add("8")
item3.SubItems.Add("9")

' Create columns for the items and subitems.
ListView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left)
ListView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left)
ListView1.Columns.Add("Column 3", -2, HorizontalAlignment.Left)
ListView1.Columns.Add("Column 4", -2, HorizontalAlignment.Center)

'Add the items to the ListView.
ListView1.Items.AddRange(New ListViewItem() {item1, item2, item3})
 
G

Greg Burns

I added a ListView1 at design and added your code, seemed to work fine.

I also added...

ListView1.View = View.Details
ListView1.CheckBoxes = True

Greg
 
Q

Q

Hello Dennis,

try to change the column width to a positive figur (eg 100)
If you do this, the columns will be visible.

Don't know why but I tried it and it works!

Regards,

Q
 
G

Guest

Thanks. I feel like an idiot. I have no idea why I had the -2 in the .Add
method.

I was mainly following the example for the ListView control in the MSDN as
follows:

listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left)
listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left)
listView1.Columns.Add("Column 3", -2, HorizontalAlignment.Left)
listView1.Columns.Add("Column 4", -2, HorizontalAlignment.Center)

Do you have any idea why Microsoft put a -2 in for the Column Width? It
seemed to work ok if I created the control in code rather than at Design Time.
 
Q

Q

Hey Dennis,

it seems that the values -1 and -2 can be used.
-1 : autosize to the largest subitem
-2 : autosize to the size of the text of the column header

I have tried both -1 and -2 and they only seem to be working with a listview
created during runtime.
If you use a listview that has been created at design time, these two
settings do not seem to work.

I have no idea why they don't work!

Regards,

Q
 
G

Greg Burns

It would appear that when you set the column width in design mode there are
no items added yet (must be the order they are added in
InitializeComponent), so the autosize doesn't know what to do.

Try setting up the columns in design mode, but add the items in code. Then
reset the column width to -1 or -2. Seems to work for me.

Dim item1 As New ListViewItem("item1", 0)
' Place a check mark next to the item.
item1.Checked = True
item1.SubItems.Add("1")
item1.SubItems.Add("2")
item1.SubItems.Add("3")
Dim item2 As New ListViewItem("item2", 1)
item2.SubItems.Add("4")
item2.SubItems.Add("5")
item2.SubItems.Add("6")
Dim item3 As New ListViewItem("item3", 0)
' Place a check mark next to the item.
item3.Checked = True
item3.SubItems.Add("7")
item3.SubItems.Add("8")
item3.SubItems.Add("9")

'Add the items to the ListView.
ListView1.Items.AddRange(New ListViewItem() {item1, item2, item3})

For Each col As ColumnHeader In ListView1.Columns
col.Width = -1
Next col

Greg
 
G

Guest

Thanks to both you an Q for helping. I ddin't read in the documentation
about the -1 and -2 but it seems really strange that Microsoft would do this
instead of adding a properties like autosizeColumnHeaders, etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top