carnegieuk
asked on
How do I add an image to a subitem in a listbox in vb.net?
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.ownerdraw(VS.85).aspx
item = New ListViewItem
item.Text = vTagName
lstTags.Items.Add(item)
item.ImageIndex = 2
item.Text = vTagName
lstTags.Items.Add(item)
item.ImageIndex = 2
Change control is not a solution for you ? You can use a DataGridView instead.
ASKER
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
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
ASKER
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?