Click here to Skip to main content
15,890,438 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 408.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

 
QuestionAdd SubItem instead of Item Pin
Member 952275119-Nov-13 22:35
Member 952275119-Nov-13 22:35 
Questiondid not appear imagens in subitens Pin
vinao9-Jan-12 7:06
vinao9-Jan-12 7:06 
AnswerRe: did not appear imagens in subitens Pin
Eric-Paul22-Jan-12 0:37
Eric-Paul22-Jan-12 0:37 
GeneralRe: did not appear imagens in subitens Pin
atifamin20023-Nov-12 2:30
atifamin20023-Nov-12 2:30 
Generalabout param Pin
jason_mf6-Mar-11 21:23
jason_mf6-Mar-11 21:23 
GeneralThank you Pin
vherisse25-Feb-09 10:26
vherisse25-Feb-09 10:26 
QuestionOakListView for VB? Pin
UweSch13-Feb-08 3:35
UweSch13-Feb-08 3:35 
AnswerRe: OakListView for VB? Pin
Dan Neely10-Apr-09 10:34
Dan Neely10-Apr-09 10:34 
Questionhow to chande background color Pin
nits_bhole17-Dec-06 21:32
nits_bhole17-Dec-06 21:32 
QuestionSubitem icons not appearing Pin
bs999930-Nov-06 5:15
bs999930-Nov-06 5:15 
GeneralText color / Background color Pin
obsy1-Jul-06 4:54
obsy1-Jul-06 4:54 
GeneralRe: Text color / Background color Pin
J Sullivan13-Nov-06 16:40
J Sullivan13-Nov-06 16:40 
GeneralRe: Text color / Background color Pin
AcerDad30-Nov-06 12:29
AcerDad30-Nov-06 12:29 
QuestionWith C# 2.0 Fix, Still No Subitem Icons? Pin
EarlD11-May-06 15:51
EarlD11-May-06 15:51 
AnswerRe: With C# 2.0 Fix, Still No Subitem Icons? Pin
EarlD11-May-06 15:53
EarlD11-May-06 15:53 
GeneralRe: With C# 2.0 Fix, Still No Subitem Icons? Pin
klimb902-Jan-08 18:53
klimb902-Jan-08 18:53 
AnswerRe: With C# 2.0 Fix, Still No Subitem Icons? Pin
Bertrand Jobert27-Feb-09 1:56
Bertrand Jobert27-Feb-09 1:56 
GeneralRe: With C# 2.0 Fix, Still No Subitem Icons? Pin
danceinrain1-Mar-09 21:23
danceinrain1-Mar-09 21:23 
QuestionIndenting SubItem ? Pin
PandaPKH17-Apr-06 23:19
PandaPKH17-Apr-06 23:19 
AnswerRe: Indenting SubItem ? Pin
yazoox24-Apr-06 21:09
yazoox24-Apr-06 21:09 
GeneralRe: Indenting SubItem ? Pin
Synetech1-Oct-08 17:13
Synetech1-Oct-08 17:13 
GeneralRe: Indenting SubItem ? Pin
#realJSOP21-Feb-10 11:48
mve#realJSOP21-Feb-10 11:48 
GeneralRe: Indenting SubItem ? Pin
Synetech21-Feb-10 11:55
Synetech21-Feb-10 11:55 
QuestionHow to Find Item with Text Pin
rm_pkt2-Apr-06 19:09
rm_pkt2-Apr-06 19:09 
Generalhere is the vb.net version of this smart class Pin
ale75fly21-Mar-06 16:32
ale75fly21-Mar-06 16:32 
Imports System
Imports System.Threading
Imports System.IO
Imports System.Drawing
Imports System.Diagnostics
Imports System.Data
Imports System.Windows.Forms
Imports System.Runtime.InteropServices

Public Class OAKListview
Inherits System.Windows.forms.ListView


'namespace OAKControls
'
'Summary description for OAKListView.
'


Public Structure LV_ITEM
Public mask As UInt32
Public iItem As Int32
Public iSubItem As Int32
Public state As UInt32
Public stateMask As UInt32
Public pszText As String
Public cchTextMax As Int32
Public iImage As Int32
Public lParam As IntPtr
End Structure

Public Const LVM_FIRST As Int32 = &H1000
Public Const LVM_GETITEM As Int32 = LVM_FIRST + 5
Public Const LVM_SETITEM As Int32 = LVM_FIRST + 6
Public Const LVIF_TEXT As Int32 = &H1
Public Const LVIF_IMAGE As Int32 = &H2

Public Const LVW_FIRST As Integer = &H1000
Public Const LVM_GETEXTENDEDLISTVIEWSTYLE As Integer = LVW_FIRST + 54

Public Const LVS_EX_GRIDLINES As Integer = &H1
Public Const LVS_EX_SUBITEMIMAGES As Integer = &H2
Public Const LVS_EX_CHECKBOXES As Integer = &H4
Public Const LVS_EX_TRACKSELECT As Integer = &H8
Public Const LVS_EX_HEADERDRAGDROP As Integer = &H10
Public Const LVS_EX_FULLROWSELECT As Integer = &H20 ' applies to report mode only
Public Const LVS_EX_ONECLICKACTIVATE As Integer = &H40

'///
'/// Changing the style of listview to accept image on subitems
'///

Public Sub New()
'// Change the style of listview to accept image on subitems
Dim m As System.Windows.Forms.Message = New Message
m.HWnd = Me.Handle
m.Msg = LVM_GETEXTENDEDLISTVIEWSTYLE
m.LParam = New IntPtr(LVS_EX_GRIDLINES Or LVS_EX_FULLROWSELECT Or LVS_EX_SUBITEMIMAGES Or LVS_EX_CHECKBOXES Or LVS_EX_TRACKSELECT)
m.WParam = IntPtr.Zero
Me.WndProc(m)
End Sub


Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" _
(ByVal hWnd As IntPtr, _
ByVal Msg As Integer, _
ByVal wParam As IntPtr, _
ByVal lParam As IntPtr) As Integer


Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" _
(ByVal hWnd As IntPtr, _
ByVal Msg As Int32, _
ByVal wParam As Int32, _
ByRef lParam As LV_ITEM) As Boolean

End Class

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.