Click here to Skip to main content
Licence CPOL
First Posted 4 Jul 2004
Views 215,525
Bookmarked 84 times

ListView with Image on SubItems

By | 4 Jul 2004 | Article
ListView with Image on SubItems

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:

/****************************************************************************/
/// 
/// 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

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)

About the Author

Carlos Carvalho

Web Developer

Australia Australia

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questiondid not appear imagens in subitens Pinmembervinao7:06 9 Jan '12  
AnswerRe: did not appear imagens in subitens PinmemberEric-Paul0:37 22 Jan '12  
Generalabout param Pinmemberjason_mf21:23 6 Mar '11  
GeneralThank you Pinmembervherisse10:26 25 Feb '09  
QuestionOakListView for VB? PinmemberUweSch3:35 13 Feb '08  
AnswerRe: OakListView for VB? Pinmemberdan neely10:34 10 Apr '09  
Questionhow to chande background color Pinmembernits_bhole21:32 17 Dec '06  
QuestionSubitem icons not appearing Pinmemberbs99995:15 30 Nov '06  
GeneralText color / Background color Pinmemberobsy4:54 1 Jul '06  
GeneralRe: Text color / Background color PinmemberJared James Sullivan16:40 13 Nov '06  
GeneralRe: Text color / Background color PinmemberAcerDad12:29 30 Nov '06  
QuestionWith C# 2.0 Fix, Still No Subitem Icons? PinmemberEarlD15:51 11 May '06  
AnswerRe: With C# 2.0 Fix, Still No Subitem Icons? PinmemberEarlD15:53 11 May '06  
GeneralRe: With C# 2.0 Fix, Still No Subitem Icons? Pinmemberklimb9018:53 2 Jan '08  
AnswerRe: With C# 2.0 Fix, Still No Subitem Icons? PinmemberBertrand Jobert1:56 27 Feb '09  
GeneralRe: With C# 2.0 Fix, Still No Subitem Icons? Pinmemberdanceinrain21:23 1 Mar '09  
QuestionIndenting SubItem ? PinmemberPandaPKH23:19 17 Apr '06  
AnswerRe: Indenting SubItem ? Pinmemberyazoox21:09 24 Apr '06  
GeneralRe: Indenting SubItem ? PinmemberSynetech17:13 1 Oct '08  
GeneralRe: Indenting SubItem ? PinmemberJohn Simmons / outlaw programmer11:48 21 Feb '10  
GeneralRe: Indenting SubItem ? PinmemberSynetech11:55 21 Feb '10  
QuestionHow to Find Item with Text Pinmemberrm_pkt19:09 2 Apr '06  
Generalhere is the vb.net version of this smart class Pinmemberale75fly16:32 21 Mar '06  
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
GeneralIcons disapearing after form hide/show Pinmemberjared james sullivan15:41 13 Feb '06  
GeneralRe: Icons disapearing after form hide/show Pinmemberjared james sullivan15:53 13 Feb '06  

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

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120529.1 | Last Updated 5 Jul 2004
Article Copyright 2004 by Carlos Carvalho
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid