BLOG.CSHARPHELPER.COM: Make a ListView control display large and small icons in C#
Make a ListView control display large and small icons in C#
The ListView control displays two sizes of icons for its items. When its View property is LargeIcon or Tile, the control displays large icons. When the View property is SmallIcon, List, or Details, the control displays small icons.
The control gets its icons from two ImageList components.
To make a ListView control display icons, follow these steps:
Add two ImageList components to the form.
Set the ImageLists' ImageSize properties to the sizes of the images they will contain. In this example, one ImageList contains 32x32 pixel images and the other contains 64x64 pixel images. (All of the images should have the same size.)
Set the ImageList's ColorDepth properties to the color depth used by the images. In this example, both ImageLists contain images that use 32-bit color so their ColorDepth properties are set to Depth32bit.
Set the ListView control's SmallImageList and LargeImageList properties to the two ImageList controls.
Add the images to the ImageList controls.
When you add an item to the ListView, set its ImageIndex property to the index of the item's image in the ImageLists. (Each item has a single ImageIndex property so its small and large images must have the same indices in the two ImageList controls.)
Make the program set the ListView's View property as desired.
The example program uses the following code to set the ListView control's View property when you select a new view from the combo box.
// Change the ListView's display style.
private void cboStyle_SelectedIndexChanged(object sender, EventArgs e)
{
switch (cboStyle.Text)
{
case "Large Icons":
lvwBooks.View = View.LargeIcon;
break;
case "Small Icons":
lvwBooks.View = View.SmallIcon;
break;
case "List":
lvwBooks.View = View.List;
break;
case "Tile":
lvwBooks.View = View.Tile;
break;
case "Details":
lvwBooks.View = View.Details;
break;
}
}
To learn about other details, such as how the program creates the ListView items, download the example and look at the code.
Comments