Solved

How do I add an image to a subitem in a listbox in vb.net?

Posted on 2009-05-19
6
967 Views
Last Modified: 2013-11-27
Hello,

I'm having some problem adding images to a sub item in a listview. I have understood that it's not  supported by default.

What is the easiest way to do this?

thank you,

Peter Hillerberg
0
Comment
Question by:carnegieuk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 7

Accepted Solution

by:
thiyaguk earned 500 total points
ID: 24420317
0
 

Author Comment

by:carnegieuk
ID: 24420543
Thats for C# isn't it?

Even if i manage to convert the code i don't really understand how to implement it, isn't their an better way or at least easier?

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24422613
Another option is to "owner draw" your ListView...which means YOU manage the DRAWING of the items yourself:
http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.ownerdraw(VS.85).aspx
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:RamanaChoudary
ID: 24423248
                           item = New ListViewItem
                            item.Text = vTagName
                            lstTags.Items.Add(item)
                            item.ImageIndex = 2
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24426379
Change control is not a solution for you ? You can use a DataGridView instead.
0
 

Author Comment

by:carnegieuk
ID: 24441188
Hello,

RamanaChoudary, thank you but that won't add images to sub items.
jpaulino: thanks but, I think that will just be more complicated because of how i want to use it.

I have tried to get this OAKListview to work. but it seams like something is missing because it wont add the images to the subitems.

I have probably just made some simple mistake, i'm not to used to converting c# to VB and implement new controls.

I attached the code from OAKListView.vb, Form1.vb and Form1.designer.vb.

Thank you for all help!
//Peter
form1.designer.vb
___________________________
 
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form
 
    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub
 
    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer
 
    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1))
        Me.ListView1 = New OAKControls.OAKListView
        Me.Button1 = New System.Windows.Forms.Button
        Me.ImageList1 = New System.Windows.Forms.ImageList(Me.components)
        Me.SuspendLayout()
        '
        'ListView1
        '
        Me.ListView1.LargeImageList = Me.ImageList1
        Me.ListView1.Location = New System.Drawing.Point(57, 12)
        Me.ListView1.Name = "ListView1"
        Me.ListView1.Size = New System.Drawing.Size(154, 181)
        Me.ListView1.SmallImageList = Me.ImageList1
        Me.ListView1.TabIndex = 0
        Me.ListView1.UseCompatibleStateImageBehavior = False
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(49, 209)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(143, 43)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Button1"
        Me.Button1.UseVisualStyleBackColor = True
        '
        'ImageList1
        '
        Me.ImageList1.ImageStream = CType(resources.GetObject("ImageList1.ImageStream"), System.Windows.Forms.ImageListStreamer)
        Me.ImageList1.TransparentColor = System.Drawing.Color.Transparent
        Me.ImageList1.Images.SetKeyName(0, "green.bmp")
        Me.ImageList1.Images.SetKeyName(1, "red.bmp")
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.ListView1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)
 
    End Sub
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents ImageList1 As System.Windows.Forms.ImageList
    Friend WithEvents ListView1 As OAKControls.OAKListView
 
End Class
 
______________________________
Form1.vb
______________________________
 
Imports WindowsApplication1.OAKControls.OAKListView
Imports WindowsApplication1.OAKControls
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Globalization
Public Class Form1
    Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        ListView1.View = Windows.Forms.View.Details
        ListView1.Columns.Add("Service", 100, HorizontalAlignment.Left)
        ListView1.Columns.Add("Clients", 100, HorizontalAlignment.Left)
        ListView1.Columns.Add("Server", 100, HorizontalAlignment.Left)
        ListView1.Columns.Add("Service Status", 100, HorizontalAlignment.Left)
 
        Dim lstStuff As ListViewItem = New ListViewItem()
 
        lstStuff.Text = "test"
        lstStuff.SubItems.Add("Second Columns Output")
        lstStuff.SubItems.Add("Third Columns Output")
        lstStuff.SubItems.Add("Fourth Columns Output")
        ListView1.Items.Add(lstStuff)
 
 
        For x As Integer = 0 To ListView1.Items.Count
            For I As Integer = 0 To ListView1.Columns.Count - 1
                Dim lvi As New OAKListView.LV_ITEM()
                ' Row
                lvi.iItem = x
                ' Column
                lvi.iSubItem = I
                lvi.pszText = "OAKListView-" & I.ToString()
                lvi.mask = OAKListView.LVIF_IMAGE Or OAKListView.LVIF_TEXT
                ' Image index on imagelist
                lvi.iImage = I
                OAKListView.SendMessage(ListView1.Handle, OAKListView.LVM_SETITEM, 0, lvi)
            Next
        Next
 
        MessageBox.Show("Tchanannnnn....", "OAKListView", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
    End Sub
 
 
End Class
 
__________________________
OAKListView.vb
__________________________
 
'************************************************************************
'OAKListView 1.0
'10/02/2004
'Developer: Carlos Carvalho
'carvalho@flag.com.br
'+55 31 99440862 / +55 31 32616977
'*************************************************************************
 
 
Imports System
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
 
Namespace OAKControls
    ''' <summary>
    ''' Summary description for OAKListView.
    ''' </summary>
    Public Class OAKListView
        Inherits ListView
        <StructLayoutAttribute(LayoutKind.Sequential)> _
        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
 
        ''' <summary>
        ''' Changing the style of listview to accept image on subitems
        ''' </summary>
        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
 
        ' handle to destination window 
        ' message 
        ' first message parameter 
        <DllImport("user32.dll")> _
        Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
        End Function
        ' second message parameter 
 
        ' handle to destination window 
        ' message 
        <DllImport("user32.dll")> _
        Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Int32, ByVal wParam As Int32, ByRef lParam As LV_ITEM) As Boolean
        End Function
        ' pointer to struct of LV_ITEM
    End Class
End Namespace

Open in new window

0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question