470,596 Members | 1,473 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,596 developers. It's quick & easy.

Listview SubItems image index

This is probably a silly question but oh well, I can't find the answer
looking via code.

Having an imagelist already, how does one set an icon for a list view's sub
items?

I'm using the code below however the subitems doesn't have an overload
allowing an image index.

Adam

***Code***

lvItem = lvControl.Items.Add(sData, 0)

sData = ReadINIFileValue(INI_SECTION_TYPE1 + " " + CType(iCounter, String),
"Program name", MyAccountsINIFilePath)

lvItem.SubItems.Add(sData) <-------- Cannot add image index here
Apr 19 '06 #1
10 11821
Adam,

The simplest method to find this kind of methods is to look how it is done
in the designer.
Therefore create a small project, do this using the designer and see how
they done it. I have not seen yet code made by the designer that was stupid
done.

I hope this helps,

Cor

"Adam Honek" <Ad*******@Webmaster2001.freeserve.co.uk> schreef in bericht
news:eN**************@TK2MSFTNGP02.phx.gbl...
This is probably a silly question but oh well, I can't find the answer
looking via code.

Having an imagelist already, how does one set an icon for a list view's
sub items?

I'm using the code below however the subitems doesn't have an overload
allowing an image index.

Adam

***Code***

lvItem = lvControl.Items.Add(sData, 0)

sData = ReadINIFileValue(INI_SECTION_TYPE1 + " " + CType(iCounter,
String), "Program name", MyAccountsINIFilePath)

lvItem.SubItems.Add(sData) <-------- Cannot add image index here

Apr 19 '06 #2
AFAIK, .NET does not built-in support for that. As alternative, see:

ListView with Image on SubItems
http://www.codeproject.com/cs/miscctrl/OAKListView.asp

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com
"Adam Honek" <Ad*******@Webmaster2001.freeserve.co.uk> escribi en el
mensaje news:eN**************@TK2MSFTNGP02.phx.gbl...
This is probably a silly question but oh well, I can't find the answer
looking via code.

Having an imagelist already, how does one set an icon for a list view's
sub items?

I'm using the code below however the subitems doesn't have an overload
allowing an image index.

Adam

***Code***

lvItem = lvControl.Items.Add(sData, 0)

sData = ReadINIFileValue(INI_SECTION_TYPE1 + " " + CType(iCounter,
String), "Program name", MyAccountsINIFilePath)

lvItem.SubItems.Add(sData) <-------- Cannot add image index here

Apr 19 '06 #3
Hello!
We are currently working on a PocketPc application in vb.net 2005.It is
reqired that we use a ListView , but with images in the sub items.
I tried the link below...

http://www.codeproject.com/cs/miscctrl/OAKListView.asp

I has some problems when trying to convert the c# code to VB.

In particular the SendMessage() function call in the Button_clickevent
throws a System.NotSupportedException ...

Could anyone suggest a solution or provide a link with VB.NET coding for the
above problem..?

Thanks,
"Adam Honek" wrote:
This is probably a silly question but oh well, I can't find the answer
looking via code.

Having an imagelist already, how does one set an icon for a list view's sub
items?

I'm using the code below however the subitems doesn't have an overload
allowing an image index.

Adam

***Code***

lvItem = lvControl.Items.Add(sData, 0)

sData = ReadINIFileValue(INI_SECTION_TYPE1 + " " + CType(iCounter, String),
"Program name", MyAccountsINIFilePath)

lvItem.SubItems.Add(sData) <-------- Cannot add image index here

Apr 19 '06 #4
Hello!
We are currently working on a PocketPC application in vb.net 2005.It is
reqired that we use a ListView , but with images in the sub items.
I tried the link below...

http://www.codeproject.com/cs/miscctrl/OAKListView.asp

I has some problems when trying to convert the c# code to VB.

In particular the SendMessage() function call in the Button_clickevent
throws a System.NotSupportedException ...

Could anyone suggest a solution or provide a link with VB.NET coding for the
above problem..?
Thank You,
"Carlos J. Quintero [VB MVP]" wrote:
AFAIK, .NET does not built-in support for that. As alternative, see:

ListView with Image on SubItems
http://www.codeproject.com/cs/miscctrl/OAKListView.asp

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com
"Adam Honek" <Ad*******@Webmaster2001.freeserve.co.uk> escribió en el
mensaje news:eN**************@TK2MSFTNGP02.phx.gbl...
This is probably a silly question but oh well, I can't find the answer
looking via code.

Having an imagelist already, how does one set an icon for a list view's
sub items?

I'm using the code below however the subitems doesn't have an overload
allowing an image index.

Adam

***Code***

lvItem = lvControl.Items.Add(sData, 0)

sData = ReadINIFileValue(INI_SECTION_TYPE1 + " " + CType(iCounter,
String), "Program name", MyAccountsINIFilePath)

lvItem.SubItems.Add(sData) <-------- Cannot add image index here


Apr 19 '06 #5
The following should work (hope I remembered everything). You can change
Friend to Public or so if you want:

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> Friend
Structure LVITEM
Friend mask As Integer
Friend iItem As Integer
Friend subItem As Integer
Friend state As Integer
Friend stateMask As Integer
<MarshalAs(UnmanagedType.LPTStr)> Friend lpszText As String
Friend cchTextMax As Integer
Friend iImage As Integer
Friend lParam As IntPtr
Friend iIndent As Integer
End Structure

Friend Const LVM_FIRST As Integer = &H1000
Friend Const LVM_GETITEMA As Integer = LVM_FIRST + 5
Friend Const LVM_GETITEMW As Integer = LVM_FIRST + 75
Friend Shared ReadOnly LVM_GETITEM As Integer =
CInt(IIf(Marshal.SystemDefaultCharSize = 1, LVM_GETITEMA, LVM_GETITEMW))
Friend Const LVM_SETITEMA As Integer = LVM_FIRST + 6
Friend Const LVM_SETITEMW As Integer = LVM_FIRST + 76
Friend Shared ReadOnly LVM_SETITEM As Integer =
CInt(IIf(Marshal.SystemDefaultCharSize = 1, LVM_SETITEMA, LVM_SETITEMW))

Friend Const LVM_SETEXTENDEDLISTVIEWSTYLE As Integer = LVM_FIRST + 54

Friend Const LVIF_IMAGE As Integer = &H2
Friend Const LVS_EX_SUBITEMIMAGES As Integer = &H2

Friend Shared Function ListView_GetItem(ByVal hwnd As IntPtr, ByRef lvi As
Win32.Native.LVITEM) As Boolean
Return CBool(Win32.Native.SendMessage(hwnd, Win32.Native.LVM_GETITEM,
IntPtr.Zero, lvi))
End Function

Friend Overloads Declare Auto Function SendMessage Lib "User32.dll" (ByVal
hwnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByRef lParam
As Win32.Native.LVITEM) As Integer
Friend Overloads Declare Auto Function SendMessage Lib "User32.dll" (ByVal
hwnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam
As Integer) As Integer

Friend Shared Sub ListView_SetSubItemImageIndex(ByVal hwnd As IntPtr, ByVal
index As Integer, ByVal subItemIndex As Integer, ByVal imageIndex As
Integer)
Dim lvi As LVITEM
lvi.iItem = index
lvi.subItem = subItemIndex
lvi.iImage = imageIndex
lvi.mask = LVIF_IMAGE
ListView_SetItem(hwnd, lvi)
End Sub

Friend Shared Function ListView_GetSubItemImageIndex(ByVal hwnd As IntPtr,
ByVal index As Integer, ByVal subItemIndex As Integer) As Integer
Dim lvi As LVITEM
lvi.iItem = index
lvi.subItem = subItemIndex
lvi.mask = LVIF_IMAGE
If ListView_GetItem(hwnd, lvi) Then
Return lvi.iImage
Else
Return -1
End If
End Function
You need the following call to enable support for sub item images (put it in
a CreateControl event handler or override)
SendMessage(myListView.Handle, LVM_SETEXTENDEDLISTVIEWSTYLE,
LVS_EX_SUBITEMIMAGES, LVS_EX_SUBITEMIMAGES)
/claes

"Adam Honek" <Ad*******@Webmaster2001.freeserve.co.uk> wrote in message
news:eN**************@TK2MSFTNGP02.phx.gbl...
This is probably a silly question but oh well, I can't find the answer
looking via code.

Having an imagelist already, how does one set an icon for a list view's
sub items?

I'm using the code below however the subitems doesn't have an overload
allowing an image index.

Adam

***Code***

lvItem = lvControl.Items.Add(sData, 0)

sData = ReadINIFileValue(INI_SECTION_TYPE1 + " " + CType(iCounter,
String), "Program name", MyAccountsINIFilePath)

lvItem.SubItems.Add(sData) <-------- Cannot add image index here

Apr 19 '06 #6
Hmm I think something's missing from this code somewhere such as no
reference to Marshal???

It's odd how VB6 had icons in listview subitems but not .Net.

I understand that we need to do the sendmessage() say in a form_load to
activate the option in our listview
in .Net as stated below but how do we actually add the image to the subitem?

Do we need to call ListView_SetSubItemImageIndex() after adding the subitem
to the LV as normal?

Thanks,
Adam

"Claes Bergefall" <lo*****@nospam.nospam> wrote in message
news:OM**************@TK2MSFTNGP02.phx.gbl...
The following should work (hope I remembered everything). You can change
Friend to Public or so if you want:

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> Friend
Structure LVITEM
Friend mask As Integer
Friend iItem As Integer
Friend subItem As Integer
Friend state As Integer
Friend stateMask As Integer
<MarshalAs(UnmanagedType.LPTStr)> Friend lpszText As String
Friend cchTextMax As Integer
Friend iImage As Integer
Friend lParam As IntPtr
Friend iIndent As Integer
End Structure

Friend Const LVM_FIRST As Integer = &H1000
Friend Const LVM_GETITEMA As Integer = LVM_FIRST + 5
Friend Const LVM_GETITEMW As Integer = LVM_FIRST + 75
Friend Shared ReadOnly LVM_GETITEM As Integer =
CInt(IIf(Marshal.SystemDefaultCharSize = 1, LVM_GETITEMA, LVM_GETITEMW))
Friend Const LVM_SETITEMA As Integer = LVM_FIRST + 6
Friend Const LVM_SETITEMW As Integer = LVM_FIRST + 76
Friend Shared ReadOnly LVM_SETITEM As Integer =
CInt(IIf(Marshal.SystemDefaultCharSize = 1, LVM_SETITEMA, LVM_SETITEMW))

Friend Const LVM_SETEXTENDEDLISTVIEWSTYLE As Integer = LVM_FIRST + 54

Friend Const LVIF_IMAGE As Integer = &H2
Friend Const LVS_EX_SUBITEMIMAGES As Integer = &H2

Friend Shared Function ListView_GetItem(ByVal hwnd As IntPtr, ByRef lvi As
Win32.Native.LVITEM) As Boolean
Return CBool(Win32.Native.SendMessage(hwnd, Win32.Native.LVM_GETITEM,
IntPtr.Zero, lvi))
End Function

Friend Overloads Declare Auto Function SendMessage Lib "User32.dll" (ByVal
hwnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByRef lParam
As Win32.Native.LVITEM) As Integer
Friend Overloads Declare Auto Function SendMessage Lib "User32.dll" (ByVal
hwnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, ByVal
lParam As Integer) As Integer

Friend Shared Sub ListView_SetSubItemImageIndex(ByVal hwnd As IntPtr,
ByVal index As Integer, ByVal subItemIndex As Integer, ByVal imageIndex As
Integer)
Dim lvi As LVITEM
lvi.iItem = index
lvi.subItem = subItemIndex
lvi.iImage = imageIndex
lvi.mask = LVIF_IMAGE
ListView_SetItem(hwnd, lvi)
End Sub

Friend Shared Function ListView_GetSubItemImageIndex(ByVal hwnd As IntPtr,
ByVal index As Integer, ByVal subItemIndex As Integer) As Integer
Dim lvi As LVITEM
lvi.iItem = index
lvi.subItem = subItemIndex
lvi.mask = LVIF_IMAGE
If ListView_GetItem(hwnd, lvi) Then
Return lvi.iImage
Else
Return -1
End If
End Function
You need the following call to enable support for sub item images (put it
in a CreateControl event handler or override)
SendMessage(myListView.Handle, LVM_SETEXTENDEDLISTVIEWSTYLE,
LVS_EX_SUBITEMIMAGES, LVS_EX_SUBITEMIMAGES)
/claes

"Adam Honek" <Ad*******@Webmaster2001.freeserve.co.uk> wrote in message
news:eN**************@TK2MSFTNGP02.phx.gbl...
This is probably a silly question but oh well, I can't find the answer
looking via code.

Having an imagelist already, how does one set an icon for a list view's
sub items?

I'm using the code below however the subitems doesn't have an overload
allowing an image index.

Adam

***Code***

lvItem = lvControl.Items.Add(sData, 0)

sData = ReadINIFileValue(INI_SECTION_TYPE1 + " " + CType(iCounter,
String), "Program name", MyAccountsINIFilePath)

lvItem.SubItems.Add(sData) <-------- Cannot add image index here


Apr 20 '06 #7


Hello!
I’m working on a Vb.net application tageting the PocketPc. So, it is
required that we write the code on .NetCompactFramework.

I need to create a ListView with 4 coulumns. It must have images on the
first 3 columns and text on the 4th.

Thank you for your code..I tried using your VB code…for my POcketPC
application in VB.Net (.NetCompactFramework) But was stuck with a few
errors…Can anybody help me out??
A few features like <marshal as> are not available in the
..NetCompactFrameWork.
So I had to make some modifications to your code…

Imports System.Runtime.InteropServices
------------------------------------------------------------------------------------------------
‘This class has a function to return the handle to any control
‘Since mylistview.Handle is not available in .NetcompactFramework
Class WinAPI

Public Declare Function SetCapture Lib "coredll.dll" (ByVal hWnd As
IntPtr) As IntPtr
Public Declare Function GetCapture Lib "coredll.dll" () As IntPtr

Public Shared Function GetHWnd(ByVal ctrl As Control) As IntPtr
Dim hOldWnd As IntPtr = GetCapture()
ctrl.Capture = True
Dim hWnd As IntPtr = GetCapture()
ctrl.Capture = False
SetCapture(hOldWnd)
Return hWnd

End Function

End Class
----------------------------------------------------------------------------------------------
Public Class Form1
Inherits System.Windows.Forms.Form
Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu

'<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Friend Structure LVITEM
Friend mask As Integer
Friend iItem As Integer
Friend subItem As Integer
Friend state As Integer
Friend stateMask As Integer
'<MarshalAs(UnmanagedType.LPTStr)>Friend lpszText As String
‘MARSHAL AS IS NOT SUPPORTED CAN I USE BYTE()????
Friend lpszText As Byte()
Friend cchTextMax As Integer
Friend iImage As Integer
Friend lParam As IntPtr
Friend iIndent As Integer
End Structure

Friend Const LVM_FIRST As Integer = &H1000
Friend Const LVM_GETITEMA As Integer = LVM_FIRST + 5
Friend Const LVM_GETITEMW As Integer = LVM_FIRST + 75
Friend Shared ReadOnly LVM_GETITEM As Integer = _
CInt(IIf(Marshal.SystemDefaultCharSize = 1, LVM_GETITEMA, LVM_GETITEMW))
Friend Const LVM_SETITEMA As Integer = LVM_FIRST + 6
Friend Const LVM_SETITEMW As Integer = LVM_FIRST + 76
Friend Shared ReadOnly LVM_SETITEM As Integer = _
CInt(IIf(Marshal.SystemDefaultCharSize = 1, LVM_SETITEMA, LVM_SETITEMW))

Friend Const LVM_SETEXTENDEDLISTVIEWSTYLE As Integer = LVM_FIRST + 54

Friend Const LVIF_IMAGE As Integer = &H2
Friend Const LVS_EX_SUBITEMIMAGES As Integer = &H2
Friend Shared Function ListView_GetItem(ByVal hwnd As IntPtr, ByRef lvi
As LVITEM) As Boolean
Return CBool(SendMessage(hwnd, LVM_GETITEM, IntPtr.Zero, lvi))
End Function

Friend Overloads Declare Function SendMessage Lib "User32.dll" (ByVal _
hwnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByRef
lParam _
As LVITEM) As Integer

Friend Overloads Declare Function SendMessage Lib "User32.dll" (ByVal _
hwnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, ByVal
lParam _
As Integer) As Integer

Friend Shared Sub ListView_SetSubItemImageIndex(ByVal hwnd As IntPtr,
ByVal _
index As Integer, ByVal subItemIndex As Integer, ByVal imageIndex As _
Integer)
Dim lvi As LVITEM
lvi.iItem = index
lvi.subItem = subItemIndex
lvi.iImage = imageIndex
lvi.mask = LVIF_IMAGE
ListView_GetItem(hwnd, lvi)
End Sub

Friend Shared Function ListView_GetSubItemImageIndex(ByVal hwnd As
IntPtr, _
ByVal index As Integer, ByVal subItemIndex As Integer) As Integer
Dim lvi As LVITEM
lvi.iItem = index
lvi.subItem = subItemIndex
lvi.mask = LVIF_IMAGE
If ListView_GetItem(hwnd, lvi) Then
Return lvi.iImage
Else
Return -1
End If
End Function

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

'ImageList
'==========
Dim Img As New ImageList
Dim myImage As New Bitmap("/Program Files/ListView2/file.png")
Img.Images.Add(myImage)

'ListView
'=========
Dim MyListView As New ListView
MyListView.Location = New Point(0, 0)
MyListView.View = View.Details
MyListView.SmallImageList = Img

Dim ListItem1 As New ListViewItem
ListItem1.ImageIndex = 0

'How to define a subitem to be used in the call to
'ListView_GetSubItemImageIndex
'Dim ListSubItem1 As New ListViewItem.ListViewSubItem
'ListSubItem1.

‘Holds the handle to myListView
Dim hMyListView As IntPtr = WinAPI.GetHWnd(MyListView)

'SendMessage(MyListView.Handle, LVM_SETEXTENDEDLISTVIEWSTYLE, _
'LVS_EX_SUBITEMIMAGES, LVS_EX_SUBITEMIMAGES)

'MyListView.Handle is not available, so had to use a userdefined function to
obtain its handle...

SendMessage(hMyListView, LVM_SETEXTENDEDLISTVIEWSTYLE, _
LVS_EX_SUBITEMIMAGES, LVS_EX_SUBITEMIMAGES)

ListView_SetSubItemImageIndex(hMyListView, 0, 0, 0)

MyListView.Visible = True
End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
MyBase.Dispose(disposing)
End Sub

'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.
Private Sub InitializeComponent()
Me.MainMenu1 = New System.Windows.Forms.MainMenu()
Me.Menu = Me.MainMenu1
Me.Text = "Form1"

End Sub

#End Region

End Class

=================================================
On debugging….
Exception 1: SendMessage()
An unhandled exception of type 'System.MissingMethodException' occurred in
ListView2.exe

Exception 2: ListView_GetItem()
An unhandled exception of type 'System.NotSupportedException' occurred in
ListView2.exe
How can I use SendMessage() in the vb.net on .net compactFramework??
How to make a call to ListView_SetSubItemImageIndex() with appropriate values.
Thanks for your help and valuble inputs :)
LProgrammer
"Claes Bergefall" wrote:
The following should work (hope I remembered everything). You can change
Friend to Public or so if you want:

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> Friend
Structure LVITEM
Friend mask As Integer
Friend iItem As Integer
Friend subItem As Integer
Friend state As Integer
Friend stateMask As Integer
<MarshalAs(UnmanagedType.LPTStr)> Friend lpszText As String
Friend cchTextMax As Integer
Friend iImage As Integer
Friend lParam As IntPtr
Friend iIndent As Integer
End Structure

Friend Const LVM_FIRST As Integer = &H1000
Friend Const LVM_GETITEMA As Integer = LVM_FIRST + 5
Friend Const LVM_GETITEMW As Integer = LVM_FIRST + 75
Friend Shared ReadOnly LVM_GETITEM As Integer =
CInt(IIf(Marshal.SystemDefaultCharSize = 1, LVM_GETITEMA, LVM_GETITEMW))
Friend Const LVM_SETITEMA As Integer = LVM_FIRST + 6
Friend Const LVM_SETITEMW As Integer = LVM_FIRST + 76
Friend Shared ReadOnly LVM_SETITEM As Integer =
CInt(IIf(Marshal.SystemDefaultCharSize = 1, LVM_SETITEMA, LVM_SETITEMW))

Friend Const LVM_SETEXTENDEDLISTVIEWSTYLE As Integer = LVM_FIRST + 54

Friend Const LVIF_IMAGE As Integer = &H2
Friend Const LVS_EX_SUBITEMIMAGES As Integer = &H2

Friend Shared Function ListView_GetItem(ByVal hwnd As IntPtr, ByRef lvi As
Win32.Native.LVITEM) As Boolean
Return CBool(Win32.Native.SendMessage(hwnd, Win32.Native.LVM_GETITEM,
IntPtr.Zero, lvi))
End Function

Friend Overloads Declare Auto Function SendMessage Lib "User32.dll" (ByVal
hwnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByRef lParam
As Win32.Native.LVITEM) As Integer
Friend Overloads Declare Auto Function SendMessage Lib "User32.dll" (ByVal
hwnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam
As Integer) As Integer

Friend Shared Sub ListView_SetSubItemImageIndex(ByVal hwnd As IntPtr, ByVal
index As Integer, ByVal subItemIndex As Integer, ByVal imageIndex As
Integer)
Dim lvi As LVITEM
lvi.iItem = index
lvi.subItem = subItemIndex
lvi.iImage = imageIndex
lvi.mask = LVIF_IMAGE
ListView_SetItem(hwnd, lvi)
End Sub

Friend Shared Function ListView_GetSubItemImageIndex(ByVal hwnd As IntPtr,
ByVal index As Integer, ByVal subItemIndex As Integer) As Integer
Dim lvi As LVITEM
lvi.iItem = index
lvi.subItem = subItemIndex
lvi.mask = LVIF_IMAGE
If ListView_GetItem(hwnd, lvi) Then
Return lvi.iImage
Else
Return -1
End If
End Function
You need the following call to enable support for sub item images (put it in
a CreateControl event handler or override)
SendMessage(myListView.Handle, LVM_SETEXTENDEDLISTVIEWSTYLE,
LVS_EX_SUBITEMIMAGES, LVS_EX_SUBITEMIMAGES)
/claes

"Adam Honek" <Ad*******@Webmaster2001.freeserve.co.uk> wrote in message
news:eN**************@TK2MSFTNGP02.phx.gbl...
This is probably a silly question but oh well, I can't find the answer
looking via code.

Having an imagelist already, how does one set an icon for a list view's
sub items?

I'm using the code below however the subitems doesn't have an overload
allowing an image index.

Adam

***Code***

lvItem = lvControl.Items.Add(sData, 0)

sData = ReadINIFileValue(INI_SECTION_TYPE1 + " " + CType(iCounter,
String), "Program name", MyAccountsINIFilePath)

lvItem.SubItems.Add(sData) <-------- Cannot add image index here


Apr 20 '06 #8
Hi,
While trying to use your VB code, I found that you had written the
SendMessage as…

Friend Overloads Declare Auto Function SendMessage Lib "User32.dll" (ByVal _
hwnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByRef
lParam _
As LVITEM) As Integer

Using “auto” is not allowed in .NetCompactFramework…
I get the error message
“ The Targeted version of the .NetCompactFramework does not support using
the Ansi,Auto, or Unicode modifier”

So I used it as
Friend Overloads Declare Function SendMessage Lib "User32.dll" (ByVal _
hwnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByRef
lParam _
As LVITEM) As Integer

Could My not using “auto” result in “An unhandled exception of type
'System.MissingMethodException' occurred in ListView2.exe”

I’m stuck… Please shed some light on this …

Thanks,
"Claes Bergefall" wrote:
The following should work (hope I remembered everything). You can change
Friend to Public or so if you want:

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> Friend
Structure LVITEM
Friend mask As Integer
Friend iItem As Integer
Friend subItem As Integer
Friend state As Integer
Friend stateMask As Integer
<MarshalAs(UnmanagedType.LPTStr)> Friend lpszText As String
Friend cchTextMax As Integer
Friend iImage As Integer
Friend lParam As IntPtr
Friend iIndent As Integer
End Structure

Friend Const LVM_FIRST As Integer = &H1000
Friend Const LVM_GETITEMA As Integer = LVM_FIRST + 5
Friend Const LVM_GETITEMW As Integer = LVM_FIRST + 75
Friend Shared ReadOnly LVM_GETITEM As Integer =
CInt(IIf(Marshal.SystemDefaultCharSize = 1, LVM_GETITEMA, LVM_GETITEMW))
Friend Const LVM_SETITEMA As Integer = LVM_FIRST + 6
Friend Const LVM_SETITEMW As Integer = LVM_FIRST + 76
Friend Shared ReadOnly LVM_SETITEM As Integer =
CInt(IIf(Marshal.SystemDefaultCharSize = 1, LVM_SETITEMA, LVM_SETITEMW))

Friend Const LVM_SETEXTENDEDLISTVIEWSTYLE As Integer = LVM_FIRST + 54

Friend Const LVIF_IMAGE As Integer = &H2
Friend Const LVS_EX_SUBITEMIMAGES As Integer = &H2

Friend Shared Function ListView_GetItem(ByVal hwnd As IntPtr, ByRef lvi As
Win32.Native.LVITEM) As Boolean
Return CBool(Win32.Native.SendMessage(hwnd, Win32.Native.LVM_GETITEM,
IntPtr.Zero, lvi))
End Function

Friend Overloads Declare Auto Function SendMessage Lib "User32.dll" (ByVal
hwnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByRef lParam
As Win32.Native.LVITEM) As Integer
Friend Overloads Declare Auto Function SendMessage Lib "User32.dll" (ByVal
hwnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam
As Integer) As Integer

Friend Shared Sub ListView_SetSubItemImageIndex(ByVal hwnd As IntPtr, ByVal
index As Integer, ByVal subItemIndex As Integer, ByVal imageIndex As
Integer)
Dim lvi As LVITEM
lvi.iItem = index
lvi.subItem = subItemIndex
lvi.iImage = imageIndex
lvi.mask = LVIF_IMAGE
ListView_SetItem(hwnd, lvi)
End Sub

Friend Shared Function ListView_GetSubItemImageIndex(ByVal hwnd As IntPtr,
ByVal index As Integer, ByVal subItemIndex As Integer) As Integer
Dim lvi As LVITEM
lvi.iItem = index
lvi.subItem = subItemIndex
lvi.mask = LVIF_IMAGE
If ListView_GetItem(hwnd, lvi) Then
Return lvi.iImage
Else
Return -1
End If
End Function
You need the following call to enable support for sub item images (put it in
a CreateControl event handler or override)
SendMessage(myListView.Handle, LVM_SETEXTENDEDLISTVIEWSTYLE,
LVS_EX_SUBITEMIMAGES, LVS_EX_SUBITEMIMAGES)
/claes

"Adam Honek" <Ad*******@Webmaster2001.freeserve.co.uk> wrote in message
news:eN**************@TK2MSFTNGP02.phx.gbl...
This is probably a silly question but oh well, I can't find the answer
looking via code.

Having an imagelist already, how does one set an icon for a list view's
sub items?

I'm using the code below however the subitems doesn't have an overload
allowing an image index.

Adam

***Code***

lvItem = lvControl.Items.Add(sData, 0)

sData = ReadINIFileValue(INI_SECTION_TYPE1 + " " + CType(iCounter,
String), "Program name", MyAccountsINIFilePath)

lvItem.SubItems.Add(sData) <-------- Cannot add image index here


Apr 20 '06 #9
Kind of hard to tell since I have no experience at all with PocketPC or the
compact framework
Verify that the method is really called SendMessage (might be SendMessageA
or SendMessageW) and that it really is located in user32.dll (it is on
normal windows, maybe it's different on PocketPC).

As for the MarshalAs attribute, just try removing it (the String datatype is
still supported I hope)

/claes

"LProgrammer" <LP*********@discussions.microsoft.com> wrote in message
news:DC**********************************@microsof t.com...
Hi,
While trying to use your VB code, I found that you had written the
SendMessage as.

Friend Overloads Declare Auto Function SendMessage Lib "User32.dll" (ByVal
_
hwnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByRef
lParam _
As LVITEM) As Integer

Using "auto" is not allowed in .NetCompactFramework.
I get the error message
" The Targeted version of the .NetCompactFramework does not support using
the Ansi,Auto, or Unicode modifier"

So I used it as
Friend Overloads Declare Function SendMessage Lib "User32.dll" (ByVal _
hwnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByRef
lParam _
As LVITEM) As Integer

Could My not using "auto" result in "An unhandled exception of type
'System.MissingMethodException' occurred in ListView2.exe"

I'm stuck. Please shed some light on this .

Thanks,
"Claes Bergefall" wrote:
The following should work (hope I remembered everything). You can change
Friend to Public or so if you want:

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> Friend
Structure LVITEM
Friend mask As Integer
Friend iItem As Integer
Friend subItem As Integer
Friend state As Integer
Friend stateMask As Integer
<MarshalAs(UnmanagedType.LPTStr)> Friend lpszText As String
Friend cchTextMax As Integer
Friend iImage As Integer
Friend lParam As IntPtr
Friend iIndent As Integer
End Structure

Friend Const LVM_FIRST As Integer = &H1000
Friend Const LVM_GETITEMA As Integer = LVM_FIRST + 5
Friend Const LVM_GETITEMW As Integer = LVM_FIRST + 75
Friend Shared ReadOnly LVM_GETITEM As Integer =
CInt(IIf(Marshal.SystemDefaultCharSize = 1, LVM_GETITEMA, LVM_GETITEMW))
Friend Const LVM_SETITEMA As Integer = LVM_FIRST + 6
Friend Const LVM_SETITEMW As Integer = LVM_FIRST + 76
Friend Shared ReadOnly LVM_SETITEM As Integer =
CInt(IIf(Marshal.SystemDefaultCharSize = 1, LVM_SETITEMA, LVM_SETITEMW))

Friend Const LVM_SETEXTENDEDLISTVIEWSTYLE As Integer = LVM_FIRST + 54

Friend Const LVIF_IMAGE As Integer = &H2
Friend Const LVS_EX_SUBITEMIMAGES As Integer = &H2

Friend Shared Function ListView_GetItem(ByVal hwnd As IntPtr, ByRef lvi
As
Win32.Native.LVITEM) As Boolean
Return CBool(Win32.Native.SendMessage(hwnd, Win32.Native.LVM_GETITEM,
IntPtr.Zero, lvi))
End Function

Friend Overloads Declare Auto Function SendMessage Lib "User32.dll"
(ByVal
hwnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByRef
lParam
As Win32.Native.LVITEM) As Integer
Friend Overloads Declare Auto Function SendMessage Lib "User32.dll"
(ByVal
hwnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, ByVal
lParam
As Integer) As Integer

Friend Shared Sub ListView_SetSubItemImageIndex(ByVal hwnd As IntPtr,
ByVal
index As Integer, ByVal subItemIndex As Integer, ByVal imageIndex As
Integer)
Dim lvi As LVITEM
lvi.iItem = index
lvi.subItem = subItemIndex
lvi.iImage = imageIndex
lvi.mask = LVIF_IMAGE
ListView_SetItem(hwnd, lvi)
End Sub

Friend Shared Function ListView_GetSubItemImageIndex(ByVal hwnd As
IntPtr,
ByVal index As Integer, ByVal subItemIndex As Integer) As Integer
Dim lvi As LVITEM
lvi.iItem = index
lvi.subItem = subItemIndex
lvi.mask = LVIF_IMAGE
If ListView_GetItem(hwnd, lvi) Then
Return lvi.iImage
Else
Return -1
End If
End Function
You need the following call to enable support for sub item images (put it
in
a CreateControl event handler or override)
SendMessage(myListView.Handle, LVM_SETEXTENDEDLISTVIEWSTYLE,
LVS_EX_SUBITEMIMAGES, LVS_EX_SUBITEMIMAGES)
/claes

"Adam Honek" <Ad*******@Webmaster2001.freeserve.co.uk> wrote in message
news:eN**************@TK2MSFTNGP02.phx.gbl...
> This is probably a silly question but oh well, I can't find the answer
> looking via code.
>
> Having an imagelist already, how does one set an icon for a list view's
> sub items?
>
> I'm using the code below however the subitems doesn't have an overload
> allowing an image index.
>
> Adam
>
> ***Code***
>
> lvItem = lvControl.Items.Add(sData, 0)
>
> sData = ReadINIFileValue(INI_SECTION_TYPE1 + " " + CType(iCounter,
> String), "Program name", MyAccountsINIFilePath)
>
> lvItem.SubItems.Add(sData) <-------- Cannot add image index here
>
>


Apr 20 '06 #10

"Adam Honek" <Ad*******@Webmaster2001.freeserve.co.uk> wrote in message
news:un**************@TK2MSFTNGP02.phx.gbl...
Hmm I think something's missing from this code somewhere such as no
reference to Marshal???
Not sure what you mean. Marshal (and MarshalAs attribute) is a framework
class

It's odd how VB6 had icons in listview subitems but not .Net.

I understand that we need to do the sendmessage() say in a form_load to
activate the option in our listview
in .Net as stated below but how do we actually add the image to the
subitem?

Do we need to call ListView_SetSubItemImageIndex() after adding the
subitem to the LV as normal?


Yes
/claes
Apr 20 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by John Lauwers | last post: by
reply views Thread by Mike Barrett | last post: by
reply views Thread by Mike Barrett | last post: by
6 posts views Thread by Jack | last post: by
2 posts views Thread by mrmagoo | last post: by
11 posts views Thread by Alan T | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.