Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

like image 365
Pete Hilde Avatar asked Feb 15 '18 10:02

Pete Hilde


People also ask

How do I sort items in ListView?

Click the various column headers in the ListView control. When you click the header, the contents of the ListView control are sorted in ascending order based on the column that you click. When you click the same column header again, that column is sorted in descending order.

What is a ListView control?

List view (ListView) controls are used to display a collection of items, each having a text label and, optionally, an icon and a check box. List view can display items in several modes - with full-sized icons or small icons, with additional information displayed in columns on the right of the item, and so on.

What is ListView control in C#?

A ListView control allows you to display a list of items with item text and, optionally, an icon to identify the type of item. For example, the Windows Explorer list of files is similar in appearance to a ListView control. It displays a list of the files and folders currently selected in the tree.

What is list view in Windows?

A list-view control is a window that displays a collection of items. List-view controls provide several ways to arrange and display items and are much more flexible than simple List Boxes. For example, additional information about each item can be displayed in columns to the right of the icon and label.


1 Answers

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;
like image 174
Reza Aghaei Avatar answered Oct 22 '22 07:10

Reza Aghaei