Click here to Skip to main content
15,884,836 members
Articles / Programming Languages / C#
Article

ListView with Image on SubItems

Rate me:
Please Sign up or sign in to vote.
4.37/5 (42 votes)
4 Jul 2004CPOL 407.2K   14.6K   94   58
ListView with Image on SubItems

Image 1

Introduction

This article describes how to implement ListView with image on subitems. It's very useful in programs that need to show the status of several items.

OAKListView control

This class inherits from the ListView class.

Using the code

Make some change in your code. Add a traditional ListView control to your form and make the changes (in black) as below:

C#
/****************************************************************************/
/// 
/// Summary description for Form1.
/// 
public class Form1 : System.Windows.Forms.Form
{
    // Change this line...
    private System.Windows.Forms.ListView listView1;
    private System.Windows.Forms.ColumnHeader columnHeader1;
    private System.Windows.Forms.ColumnHeader columnHeader2;

/// 
/// Summary description for Form1.
/// 
public class Form1 : System.Windows.Forms.Form
{
    // Like this line...
    private OAKControls.OAKListView listView1;
    private System.Windows.Forms.ColumnHeader columnHeader1;
    private System.Windows.Forms.ColumnHeader columnHeader2;

/****************************************************************************/

/****************************************************************************/
#region Windows Form Designer generated code
/// 
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// 
private void InitializeComponent()
{
    this.components = new System.ComponentModel.Container();
    System.Resources.ResourceManager resources = 
      new System.Resources.ResourceManager(typeof(Form1));
    // Change this line...
    this.listView1 = new System.Windows.Forms.ListView();

#region Windows Form Designer generated code
/// 
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// 
private void InitializeComponent()
{
    this.components = new System.ComponentModel.Container();
    System.Resources.ResourceManager resources = 
      new System.Resources.ResourceManager(typeof(Form1));
    // Like this line...
    this.listView1 = new OAKControls.OAKListView();

/*****************************************************************************/

Changing image on subitem

C#
OAKListView.LV_ITEM lvi = new OAKListView.LV_ITEM();
// Row of the item
lvi.iItem = 5; 
// Column of the item
lvi.iSubItem = 5;
lvi.pszText = "OAKListView-" + I.ToString();
//Here you pass the mask of the item. In this case 
//the mask is the both, Image and the text
lvi.mask = OAKListView.LVIF_IMAGE | OAKListView.LVIF_TEXT; 
// Image index on imagelist
lvi.iImage = 1;
// Sending a message to make the changes
OAKListView.SendMessage(listView1.Handle, 
         OAKListView.LVM_SETITEM, 0, ref lvi);

Conclusion

Most of the functionalities have been described here.

I do not guarantee that this control works 100%. If you find mistakes, you can correct them. This control is not a final release and you can send me mails with explanations if you find bugs or mistakes, and join the modifications if you did them.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Australia Australia
Carlos Carvalho is a developer and trainer based in Brasil.
He has been developing applications and components since 1995.
He has a large experience in development of several platforms including embedded systems.

Comments and Discussions

 
GeneralNo Image shown in SubItems column in c# 2.0 Pin
Donald Man6-Oct-05 21:45
Donald Man6-Oct-05 21:45 
GeneralRe: No Image shown in SubItems column in c# 2.0 Pin
klemenz3-Nov-05 5:28
klemenz3-Nov-05 5:28 
GeneralRe: No Image shown in SubItems column in c# 2.0 Pin
vovototo24-Dec-05 1:21
vovototo24-Dec-05 1:21 
GeneralRe: No Image shown in SubItems column in c# 2.0 Pin
Dustin Aleksiuk13-Jan-06 13:52
Dustin Aleksiuk13-Jan-06 13:52 
GeneralRe: No Image shown in SubItems column in c# 2.0 Pin
shivpal4-Feb-06 4:50
shivpal4-Feb-06 4:50 
GeneralRe: No Image shown in SubItems column in c# 2.0 Pin
yazoox5-Apr-06 21:38
yazoox5-Apr-06 21:38 
QuestionRe: No Image shown in SubItems column in c# 2.0 Pin
Célio3-Jan-07 23:04
Célio3-Jan-07 23:04 
GeneralRe: No Image shown in SubItems column in c# 2.0 Pin
Avi zir16-May-08 10:40
Avi zir16-May-08 10:40 
GeneralRe: No Image shown in SubItems column in c# 2.0 Pin
Steven Nicholas28-Feb-09 22:33
Steven Nicholas28-Feb-09 22:33 
QuestionHow to get the text of a listview item. Pin
Dan Neely23-Sep-05 9:25
Dan Neely23-Sep-05 9:25 
GeneralAfter Sorting - Images are Gone Pin
Sumeet_CodeProject26-Jul-05 2:00
Sumeet_CodeProject26-Jul-05 2:00 
GeneralRe: After Sorting - Images are Gone Pin
Dan Neely23-Sep-05 9:06
Dan Neely23-Sep-05 9:06 
GeneralVB.Net version Pin
j1webb4-Mar-05 13:58
j1webb4-Mar-05 13:58 
GeneralJuste a detail & image crop Pin
Styx3122-Feb-05 4:27
Styx3122-Feb-05 4:27 
GeneralImagelist control Pin
Daap16-Dec-04 2:00
Daap16-Dec-04 2:00 
Generaldon't want image in first column Pin
BlackDice15-Nov-04 9:10
BlackDice15-Nov-04 9:10 
GeneralRe: don't want image in first column Pin
obsy29-Jun-06 0:53
obsy29-Jun-06 0:53 
GeneralRe: don't want image in first column Pin
BlackDice29-Jun-06 4:03
BlackDice29-Jun-06 4:03 
GeneralRe: don't want image in first column Pin
obsy29-Jun-06 8:31
obsy29-Jun-06 8:31 
GeneralRe: don't want image in first column Pin
Synetech26-Aug-10 11:19
Synetech26-Aug-10 11:19 
General5* congrats Pin
bigodes17-Oct-04 3:35
bigodes17-Oct-04 3:35 
GeneralRe: 5* congrats Pin
Anonymous14-Jan-05 5:49
Anonymous14-Jan-05 5:49 
GeneralIt's better to introduce your code Pin
newpant15-Jul-04 13:53
newpant15-Jul-04 13:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.