简体   繁体   中英

List view sort arrow vb.net

i am trying to draw sort arrows on list view column header along with the default visual styles

so far i have got this

Private Sub List_DrawColumnHeader(sender As Object, e As DrawListViewColumnHeaderEventArgs) Handles List.DrawColumnHeader

    e.DrawDefault = True
    If e.ColumnIndex = selectedIndex Then
        e.Graphics.DrawImage(ImageList1.Images(1), CType(e.Bounds.Left + e.Bounds.Width / 2, Single) - 5, -2)
    End If

End Sub

but the visual style is drawn over the arrow somehow so i figured i could try this :

Private Sub List_DrawColumnHeader(sender As Object, e As DrawListViewColumnHeaderEventArgs) Handles List.DrawColumnHeader

    e.DrawDefault = True
    If lastDrawn.ColumnIndex = selectedIndex Then
        e.Graphics.DrawImage(ImageList1.Images(1), CType(lastDrawn.Bounds.Left + lastDrawn.Bounds.Width / 2, Single) - 5, -2)
    End If

    lastDrawn=e

End Sub

and it draws the arrow when the next corresponding column is being drawn but with this i cant get it to draw for the last column

Screenshots: 在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

In order to use the .NET build in solution for showing a custom icon for a list view column header you need to:

  • create an ImageList
  • add three images (up / down arrow and empty) to it
  • bind the image list to the ListView control
  • bind to the ColumnClick event of the ListView control
  • when sorting the columns set the ImageKey property of the currently sorted column depending on the sorting direction

This example class (a simple form) shows how to set the images correctly not using custom drawing for the ListView header columns.

It does not implement any sort! (how to implement a ListViewSorter is shown in this MSDN article )

You need to implement a custom ListView-Sorter class and retrieve the image or the image key from it, after a column is sorted.

Public Class SimpleForm
    Inherits Form

    Private sortItems = New ImageList()
    Dim lv As ListView = New ListView()
    Dim so = System.Windows.Forms.SortOrder.Ascending

    Public Sub New()
        ' create columns, items and ListView
        Dim columns = New List(Of ColumnHeader)
        Dim c1 = New ColumnHeader()
        c1.Name = "c1"
        c1.Text = "Name"
        Dim c2 = New ColumnHeader()
        c2.Name = "c2"
        c2.Text = "Type"
        columns.Add(c1)
        columns.Add(c2)

        Dim items = New List(Of ListViewItem)
        Dim i1 = New ListViewItem("Terminator")
        i1.SubItems.Add("T1000")
        Dim i2 = New ListViewItem("Terminator")
        i2.SubItems.Add("T10")
        Dim i3 = New ListViewItem("J.C.")
        i3.SubItems.Add("Human")
        items.Add(i1)
        items.Add(i2)
        items.Add(i3)
        ' init and bind column click
        lv.Columns.AddRange(columns.ToArray())
        lv.Items.AddRange(items.ToArray())
        lv.SmallImageList = sortItems
        lv.View = View.Details
        lv.Dock = DockStyle.Fill
        Controls.Add(lv)

        AddHandler lv.ColumnClick, AddressOf clickEventHandler
        ' init images list 
        sortItems.TransparentColor = System.Drawing.Color.Transparent
        sortItems.Images.Add("up", Image.FromFile("d:\temp\32\arrow_up.gif"))
        sortItems.Images.Add("down", Image.FromFile("d:\temp\32\arrow_down.gif"))
        sortItems.Images.Add("empty", Image.FromFile("d:\temp\32\check.gif"))

    End Sub

    Private Sub clickEventHandler(ByVal o As Object, ByVal e As ColumnClickEventArgs)
        ' Implement a custom ListViewItemSorter and fetch the icon from it!
        ' Set the ListViewItemSorter property to a new ListViewItemComparer  
        ' object. Setting this property immediately sorts the  
        ' ListView using the ListViewItemComparer object. 
        ' THIS CODE SHOWS ONLY HOW TO SET THE SORT ICON!
        For i As Integer = 0 To lv.Columns.Count - 1
            If (i = e.Column) Then
                Select Case (so)
                    Case System.Windows.Forms.SortOrder.Ascending
                        lv.Columns(i).ImageKey = "up"
                        so = System.Windows.Forms.SortOrder.Descending
                    Case System.Windows.Forms.SortOrder.Descending
                        lv.Columns(i).ImageKey = "down"
                        so = System.Windows.Forms.SortOrder.Ascending
                    Case Else
                        lv.Columns(i).ImageKey = "empty"
                        so = System.Windows.Forms.SortOrder.None
                End Select
            Else
                lv.Columns(i).ImageKey = "empty"
            End If
        Next i
    End Sub

End Class

The output looks like this:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM