Home
C#.WinForms
ListView Examples
This page was last reviewed on Oct 5, 2022.
Dot Net Perls
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.
Control notes. By providing functionality built-in, we avoid complex layout routines. Let us begin by adding a ListView control to our Windows Forms.
Add method. 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.
Tip To do this, you can add the Load event to your containing form and then invoke the 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.
Here We place statements in 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); } } }
View. There are many different settings for the View property on the ListView. This property determines how the icons in the control are displayed.
Items. 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.
Detail You can set this to true or false to mutate the check programmatically, or read it to see if the user checked the item.
ImageList, icons. When 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.
Then Add images to the ImageList by right-clicking on it and selecting properties. Then open the Images Collection Editor dialog box.
ImageList
Activation. The term "activation" refers to how an item is chosen by the user. In other words, double-clicking or pressing Enter are ways of activating an item.
Columns. 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.
Detail The HeaderStyle property is useful when the View property is set to "Details". This affects header appearance.
Groups. The Groups property provides a way to visually separate distinct classes of items. You can add GroupItems and then change the items to reference specific groupings.
Summary. The ListView control in Windows Forms displays a file system manager-style interface. We can change the display and behavior of the control.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Oct 5, 2022 (edit).
Home
Changes
© 2007-2024 Sam Allen.