Solved

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

Posted on 2009-05-19
6
942 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
6 Comments
 
LVL 7

Accepted Solution

by:
thiyaguk earned 500 total points
Comment Utility
0
 

Author Comment

by:carnegieuk
Comment Utility
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 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 4

Expert Comment

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

Expert Comment

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

Author Comment

by:carnegieuk
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now