Pete Hilde
Pete Hilde

Reputation: 699

How to make a ListView-Control behave like a ListBox-Control?

Because I would like to color every single element differently, I decided for the ListView instead of the ListBox, which can only colour all elements at once.

That means it should, so to speak, have only one column and insert the elements among each other, comparable to the command listBox.Items.Insert(0, "Item").

Which properties do I need to change in order to achieve that?

I already tried setting the View Property to View.List but as soon as there are too many elements in a row, it continues to insert the elements into a second row that I didn't even create and also can't find when I'm looking at Edit Columns...

Upvotes: 6

Views: 2492

Answers (1)

Reza Aghaei
Reza Aghaei

Reputation: 125197

You can set the View to Details and set the HeaderStyle to None, and then by adding a column and setting its size to -1 force the column to use the same width as ListView:

this.listView1.View = View.Details;
this.listView1.HeaderStyle = ColumnHeaderStyle.None;
this.listView1.FullRowSelect = true;
this.listView1.Columns.Add("", -2);
this.listView1.Items.Add("Something");
this.listView1.Items.Add("Something else").BackColor = Color.Red;

Upvotes: 9

Related Questions