Plunging into .NET Development

Weblog Pieter Gheysens
Microsoft .NET Development - C# - Enterprise Library - Visual Studio 2005 Team System - Compuware DevPartner - ...
 


Thursday, December 16

Customizing System.Windows.Forms.ListView

In the beginning I wasn't really excited about using ListViews for my GUI, but nowadays I found them quite useful because they have more GUI-power than I was aware of, but they still have their limitations.

Some months ago, during Euro 2004 in Portugal [Yes, I am a soccer-fan], I was building a small tool to follow the tournament (see picture).

How to customize ListViews ...
  • How to make use of images
    First of all, you must create an ImageList with your chosen images. The ListView-control can display icons from three sorts of ImageLists [LargeImageList, SmallImageList and StateImageList], dependent on how you want to present your ListView.
    • The LargeImageList is set for the "LargeIcon" View of the ListView.
    • The SmallImageList is set for the "List", "Details" and "SmallIcon" view of the ListView.
    • The StateImageList is used when an additional set of icons needs to be displayed (by default a checkbox).

    In my example I have chosen to work with the SmallImageList.

    //Assign ImageList to SmallImageList-property of ListView
    this.MylistViewGroup.SmallImageList = this.imageListGroupA;

  • Creation of ListViewItem
    For each row you want to add to the ListView, you must create a ListViewItem ...

    //The constructor also defines the textValue for the first ColumnValue
    ListViewItem myListViewItem = new ListViewItem("");
    //Set ImageIndex for ListViewItem
    myListViewItem.ImageIndex = 0; //0 is the index for the image of Spain

  • Make it possible to format the ListViewSubItem
    You must set the ListViewItem-property "UseItetmStyleForSubItems" to false before the formatting of the SubItems will be displayed on screen.

    //Style for ListViewItem is not used for all SubItems
    myListViewItem.UseItemStyleForSubItems = false;

  • Create ListViewSubItems and add them to the ListViewItem
    For each column you should create a ListViewSubItem (with formatting if needed) and add this in the desired order to the ListViewItem [starting with the second column because the first column is already set via the constructor of the ListViewItem].

    //Create ListViewSubItem
    ListViewItem.ListViewSubItem lvSubItemColumn2 = new ListViewItem.ListViewSubItem();
    //Assign text for the ColumnValue
    lvSubItemColumn2.Text = "Portugal";
    //Assign ForeColor and BackColor. You can also specify a new Font if necessary
    lvSubItemColumn2.ForeColor = System.Drawing.Color.Black;
    lvSubItemColumn2.BackColor = System.Drawing.Color.WhiteSmoke;
    //Adding subItem to listViewItem
    myListViewItem.SubItems.Add(lvSubItemColumn2);
    //Add other SubItems to ListViewItem
    ...

  • Add ListViewItem to ListView
    At the end when all subItems are added to the different ListViewItems, add all ListViewItems to the ListView.

    //Add created ListViewItem to ListView
    this.MylistViewGroup.Items.Add(myListViewItem);

A real shortcoming of the ListView-control is that the image for the ListViewItem is always placed (by default) in the first column of the ListView and there is no property to change that. This means that you cannot place an image in another column and as a result you can have maximum 2 images for each ListViewItem in the same column when using the StateImageList.

The standard ListView-control supports multiple columns, which can hold unique text values. In many circumstances that simplicity is enough, but sometimes the need will arise to embed something other than text in a ListView's column. Anyone already familiar with those kind of things? Custom Controls?

0 Comments:

Post a Comment

<< Home