Home

Windows Control: The List View

 

Introduction

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, the Microsoft Windows operating system provides the list view control. A list view is used to display a list of items to the user but it can be configured to change the type of display.

 

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

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;
    }
}

Countries Statistics

After this declaration, an empty rectangular control is created and added to your application. You can then start populating it.

Practical LearningPractical Learning: Introducing the Tree View Control

  1. Start Microsoft Visual C#
  2. Create a new Windows Application named DeptStore1
  3. From the Common Controls section of the Toolbox, click ListView and click the form
  4. While the list view is still selected, in the Properties window, change the (Name) to lvwStoreItems
     
    Department Store
  5. Save the form
 

Home Copyright © 2007 FunctionX, Inc. Next