Solved

LISTVIEW - image /  icon in column header

Posted on 2003-10-26
9
526 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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 …
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now