LISTVIEW - image / icon in column header

Hi,

I need to add an arrow icon to the column header when the listview is sorted.  It should look like Outlook and other applications.  I have my pictures, but haven't been able to come up with code that works with Windows Common Controls 6.

The following link only works with Common Controls 5, so please don't post it: http://www.mvps.org/vbnet/code/comctl/lvheaderimage.htm

... unless you can tell me how to modify it to work with VB 6 & Common Controls 6.

Also, please don't post something that you find from google and have no idea what it does.  When people do this, the people with the real knowledge skip over the question, and I lose my points for the month.

Please please please help.  And thank you.

-Thunder
LVL 1
Thunder27Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gafoorgkCommented:
add an ImageList named ImageList1 with the icon to be placed in the header and do the following

With ListView1
  Set .ColumnHeaderIcons = ImageList1
 
  '// Following line has to be added after sorting
  .ColumnHeaders(<header index, in which icon has to be placed>).Icon = <icon index/key from ImageList1>
End With
0
Thunder27Author Commented:
gafoorgk... Is there any way I can eliminate the space to the left of column headers that don't have the icon currently displayed?  Or, can I right-align the icons?


Thanks!
Thunder


p.s. I increased points to everything I have.. this one should be worth 500, but it's close.
0
gafoorgkCommented:
To a form add a listview  (ListView1), ImageList (Imagelist1), and a command button (Command1).

To the imagelist, add at least two images (bitmaps, icons or gifs). I used Paint to create the two images using grey, white and dark grey. The image size should not be larger than 16x16, and more desirably around 12x12 or 13x13 pixels. The routine below uses the ListView1.SortOrder to determine the image to display - a sort order of 0 (ascending) displays the first image, and a SortOrder of 1 (descending) the second image. Therefore I put the "down" arrow as image 1, and the "up" arrow as image 2.  Important: if you followed the above instructions, set the imagelist MaskColor and backcolour values to Button Face form the Colours property page.  If the images appear with a white of black background, these settings are the problem.


Option Explicit

Private Const LVM_FIRST = &H1000
Private Const LVM_GETHEADER = (LVM_FIRST + 31)

Private Const HDI_BITMAP = &H10
Private Const HDI_IMAGE = &H20
Private Const HDI_FORMAT = &H4
Private Const HDI_TEXT = &H2

Private Const HDF_BITMAP_ON_RIGHT = &H1000
Private Const HDF_BITMAP = &H2000
Private Const HDF_IMAGE = &H800
Private Const HDF_STRING = &H4000

Private Const HDM_FIRST = &H1200
Private Const HDM_SETITEM = (HDM_FIRST + 4)
Private Const HDM_SETIMAGELIST = (HDM_FIRST + 8)
Private Const HDM_GETIMAGELIST = (HDM_FIRST + 9)

Private Type HD_ITEM
   mask As Long
   cxy As Long
   pszText As String
   hbm As Long
   cchTextMax As Long
   fmt As Long
   lParam As Long
   iImage As Long
   iOrder As Long
End Type

Private Declare Function SendMessage Lib "user32" _
   Alias "SendMessageA" _
  (ByVal hwnd As Long, _
   ByVal wMsg As Long, _
   ByVal wParam As Long, _
   lParam As Any) As Long


Private Sub Form_Load()

   Dim i As Integer
   Dim itmX As ListItem
   
  'Create and populate the listview
   With ListView1
     
      .SmallIcons = ImageList1
     
      For i = 1 To 3
         .ColumnHeaders.Add , "x" & i, "Column " & i
         .ColumnHeaders.Item(i).Width = 1520
      Next i
     
      For i = 1 To 9
         Set itmX = .ListItems.Add(, "key" & i, "Item " & i)
         itmX.SubItems(1) = "Subitem " & i
         itmX.SubItems(2) = "Subitem " & i
      Next i
     
   End With
   
  'ShowHeaderIcon colNo, imgIndex, showFlag
   ShowHeaderIcon 0, 0, True
   
End Sub


Private Sub Command1_Click()

   Unload Me
     
End Sub


Public Sub ShowHeaderIcon(colNo As Long, _
                          imgIconNo As Long, _
                          showImage As Long)

   Dim hHeader As Long
   Dim HD As HD_ITEM
   
  'get a handle to the listview header component
   hHeader = SendMessage(ListView1.hwnd, LVM_GETHEADER, 0, ByVal 0)
   
  'set up the required structure members
   With HD
      .mask = HDI_IMAGE Or HDI_FORMAT
      .pszText = ListView1.ColumnHeaders(colNo + 1).Text
     
       If showImage Then
         .fmt = HDF_STRING Or HDF_IMAGE Or HDF_BITMAP_ON_RIGHT
         .iImage = imgIconNo
       Else
         .fmt = HDF_STRING
      End If


   End With
   
  'modify the header
   Call SendMessage(hHeader, HDM_SETITEM, colNo, HD)
   
End Sub


Private Sub ListView1_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)

   Dim i As Long
   Static sOrder
   
   sOrder = Not sOrder
   
  'Use default sorting to sort the items in the list
   ListView1.SortKey = ColumnHeader.Index - 1
   ListView1.SortOrder = Abs(sOrder)
   ListView1.Sorted = True
   
  'clear the image from the headers not
  'currently selected, and update the
  'header clicked
   For i = 0 To 2
     
     'if this is the index of the header clicked
      If i = ListView1.SortKey Then
     
           'ShowHeaderIcon colNo, imgIndex, showFlag
            ShowHeaderIcon ListView1.SortKey, _
                           ListView1.SortOrder, _
                           True
                           
      Else: ShowHeaderIcon i, 0, False
      End If
   
   Next
   
End Sub
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Thunder27Author Commented:
Um... that's a copy/paste from the link I posted in the original message, except for MSComctlLib.ColumnHeader.  As I mentioned above, it doesn't work for Common Controls 6.

You'll notice that copy/pasting this code into the compiler and running will not display icons.
0
gafoorgkCommented:
oh! it's working with me. exactly as u wanted.
0
Thunder27Author Commented:
Interesting... I'll try it with a different VB 6 install..
0
PashaModCommented:
PAQed - no points refunded (of 490)

PashaMod
Community Support Moderator
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.