Windows Control: The List View
A list box is used to display a list of strings and all items of that control are primarily strings. To go a little further than a simple list of strings…
Introduction#
While a list box primarily displays a series of strings, the Windows operating system offers the list view control for more complex needs. A list view displays a collection of items that can be easily reconfigured to different display types, while keeping the original behavior intact.
List View Creation#
The list view control is made available in the .NET Framework through the ListView class that is represented in the Windows Forms section of the Toolbox by the list view button. To add a list view to your application, you can more readily click list view in the Toolbox and click the form or another container. To programmatically create a list view, you can declare a variable of type ListView, use the new operator to instantiate it and add it to its host's list of controls through a call to the Controls.Add() method. Here is an example: while keeping the original behavior intact.
using System;
using System.Drawing;
using System.Windows.Forms;
public class Exercise : System.Windows.Forms.Form
{
ListView lvwCountries;
public Exercise()
{
InitializeComponent();
}
private void InitializeComponent()
{
Text = "Countries Statistics";
Size = new Size(452, 218);
lvwCountries = new ListView();
lvwCountries.Location = new Point(12, 12);
lvwCountries.Width = 420;
lvwCountries.Height = 160;
Controls.Add(lvwCountries);
}
}
public class Program
{
static int Main()
{
System.Windows.Forms.Application.Run(new Exercise());
return 0;
}
}Once declared, an empty rectangular area is added to the application, ready for data. Practical Exercise: Introduction to the Tree View Control
- Start Microsoft Visual C#
- Create a new Windows Application named DeptStore1
- From the Common Controls section of the Toolbox, click ListView and click the form
- While the list view is still selected, in the Properties window, change the (Name) to lvwStoreItems
- Save the form
Home Copyright © 2008-2009 C# Key Next

