C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Tip: Right-click on the ListView in Visual Studio and select Properties to open this dialog.
Overview: In the following sections, we first explore the View property, and then investigate how to add items.
However: More useful, you can add elements dynamically through C# code, as in the Load event handler on the enclosing form.
Checked: You can set this to true or false to mutate the check programmatically, or read it to see if the user checked the item.
And: You can access this changed text programmatically through the Text property as well.
Next: Add images to the ImageList by right-clicking on it and selecting properties. Then open the Images Collection Editor dialog box.
Then: Whenever you want a certain image on an item in the ListView, you can simply reference an index to an image inside the ImageList.
ImageListNote: This behavior is not standard for most types of controls, but may be useful for some programs.
Tip: You can select Standard or TwoClick to get the default behavior, or OneClick to trigger activation events on every single click.
HeaderStyle: The HeaderStyle property is useful when the View property is set to "Details". This affects header appearance.
And: Fortunately, Windows Forms handles the display characteristics automatically.
However: We create a method body. This implementation detects when the selection is empty.
Example that listens for changed selections: C#
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";
}
}
}
}
Finally: Double-click on the ItemActivate to create a stub method. This example implementation is simplistic but demonstrative.
Example that acts upon clicks: C#
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);
}
}
}
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.
Also: There are more elaborate overloads to this method, which are probably more useful.
Example that populates Items: C#
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);
}
}
}
}