ListView images in header (sort indicator) -- converted code from vb6 not working

S

Samuel R. Neff

I'm trying to put icons in the header of my ListView. All the .net
posts I found refer to an example on thecodeproject which is no longer
there (part of commercial package now).

I converted the code I used in VB6 which all appears to run ok but
doesn't actually create the images ever. These are generic functions
to set the images for any listview.

Please help,

Thanks,

Sam


' Win32 is a custom class that has the necessary Win32 constants,
structures, and function declarations

Public Sub ListViewSetHeaderImages(ByVal lvw As ListView, ByVal
images As ImageList)
Dim headerHandle As IntPtr = Win32.SendMessage(lvw.Handle,
Win32.LVM_GETHEADER, IntPtr.Zero, IntPtr.Zero)
Win32.SendMessage(headerHandle, Win32.HDM_SETIMAGELIST,
IntPtr.Zero, images.Handle)
End Sub

Public Sub ListViewSetHeaderIcon(ByVal lvw As ListView, ByVal
headerText As String, ByVal columnIndex As Integer, ByVal imageIndex
As Integer)

Dim headerHandle As IntPtr
Dim HD As New Win32.HDITEM

headerHandle = Win32.SendMessage(lvw.Handle,
Win32.LVM_GETHEADER, IntPtr.Zero, IntPtr.Zero)

With HD
.mask = Win32.HDI_IMAGE Or Win32.HDI_FORMAT
.pszText = headerText
.fmt = Win32.HDF_STRING Or Win32.HDF_IMAGE Or
Win32.HDF_BITMAP_ON_RIGHT
.iImage = imageIndex
End With

Win32.SendMessage(headerHandle, Win32.HDM_SETITEM, columnIndex,
HD)

End Sub

Private Sub ListViewRemoveHeaderIcon(ByVal lvw As ListView, ByVal
columnIndex As Integer)
Dim headerHandle As IntPtr
Dim hdHeader As New Win32.HDITEM

headerHandle = Win32.SendMessage(lvw.Handle,
Win32.LVM_GETHEADER, IntPtr.Zero, IntPtr.Zero)

hdHeader.mask = Win32.HDI_FORMAT
hdHeader.fmt = hdHeader.fmt Or Win32.HDF_STRING
Win32.SendMessage(headerHandle, Win32.HDM_SETITEM, columnIndex,
hdHeader)

End Sub
 
S

Samuel R. Neff

That example does a lot more than just put images in the headers. It
takes over creation and drawing of the columns and seems to have a lot
more overhead than is necessary for the task (it does more than what I
and most people asking the question want). I'm looking for something
much smaller.

Really, the VB6 converted code I posted should work--I'm not sure why
it doesn't. It's just SendMessage calls.

Sam
 
S

Samuel R. Neff

Found the problem. The functions I posted actually do work as
intended--as only as the Win32 declarations are correct. :)

I had copied the Win32 declarations from the other project posted,
after automatically converting from C# to VB.NET. The converter
reorded the members alphabetically including the structure members
(which is really bad sine they were even marked as sequential). After
I recreated the Win32 type declaration for HDITEM the helper functions
for setting the image worked fine.

Sam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top