ListView
This control provides a useful view of items with icons. This view is similar to those found in file system managers such as Windows Explorer.
By providing functionality built-in, we avoid complex layout routines. Let us begin by adding a ListView
control to our Windows Forms.
With the ListView
control, it is possible to dynamically add Items. Alternatively we can set them at design time in Visual Studio through the interface
.
listView1.Items.Add
method upon your control instance.using System; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // When the enclosing form loads, add three string items to the ListView. // ... Use an array of strings. string[] array = { "cat", "dog", "mouse" }; var items = listView1.Items; foreach (var value in array) { items.Add(value); } } } }
SelectedIndexChanged
It is possible to listen for when the selection changes. This occurs when the user clicks the mouse or pressed another key such as up, down, left, right, or tab.
SelectedIndexChanged
—this implementation detects when the selection is empty.using System; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void listView1_SelectedIndexChanged(object sender, EventArgs e) { // Acquire SelectedItems reference. var selectedItems = listView1.SelectedItems; if (selectedItems.Count > 0) { // Display text of first item selected. this.Text = selectedItems[0].Text; } else { // Display default string. this.Text = "Empty"; } } } }
ItemActivate
How can you listen for when an item was clicked or the return key was pressed when an item was selected? You can use the ItemActivate
event handler.
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void listView1_ItemActivate(object sender, EventArgs e)
{
// Determine which item was clicked/activated.
MessageBox.Show("You clicked " + listView1.SelectedItems[0].Text);
}
}
}
The Items property enables you to add the data entries. You can add to Items by double-clicking on the Items entry and adding Items in the ListViewItem
Collection
Editor.
CheckBoxes
To add CheckBox
controls, set the CheckBoxes
property to true. The user will then be able to check or uncheck various items in the ListView
through the user interface.
ImageList
, iconsWhen you are using the ListView
, you will probably want to add icons to every item. To do this, you must first create an ImageList
control.
ImageList
by right-clicking on it and selecting properties. Then open the Images Collection
Editor dialog box.Like other Columns properties in Windows Forms, such as those upon the DataGridView
, these items serve as a template to drive the display of the data.
HeaderStyle
property is useful when the View property is set to "Details". This affects header appearance.The ListView
control in Windows Forms displays a file system manager-style interface
. We can change the display and behavior of the control.