Solved

LISTVIEW - image /  icon in column header

Posted on 2003-10-26
9
535 Views
Last Modified: 2009-07-29
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
0
Comment
Question by:Thunder27
  • 3
  • 3
9 Comments
 
LVL 3

Expert Comment

by:gafoorgk
ID: 9625192
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
 
LVL 1

Author Comment

by:Thunder27
ID: 9628777
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
 
LVL 3

Expert Comment

by:gafoorgk
ID: 9631795
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 1

Author Comment

by:Thunder27
ID: 9631873
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
 
LVL 3

Expert Comment

by:gafoorgk
ID: 9632025
oh! it's working with me. exactly as u wanted.
0
 
LVL 1

Author Comment

by:Thunder27
ID: 9634689
Interesting... I'll try it with a different VB 6 install..
0
 

Accepted Solution

by:
PashaMod earned 0 total points
ID: 10371838
PAQed - no points refunded (of 490)

PashaMod
Community Support Moderator
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
to transfer string from C lanaguage to VBA 4 72
VBA: Select SQL query based on a config Sheet v2 11 49
Visual Studio 2005 text editor 10 44
Automatic Email Reminder 4 58
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

830 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