none
Changing color of Listview header, gridlines and scrollbar RRS feed

  • Question

  • Hi

    Not sure if this is possible - even with OwnerDraw? but want to change colour of listview header, gridlines and scrollbar to match other third party skin I am using

    Or recommendation to a good free listview alternative that does allow it?

    Done a lot of searching but most results quite old, so thought I would ask the experts!!

    Thank you


    Darren Rose

    Sunday, January 26, 2020 5:33 PM

All replies

  • The LV Header can be OD

    Quick test :

    Sunday, January 26, 2020 6:02 PM
  • The LV Header can be OD

    Quick test :

    Okay, what about grid line colour and scrollbar?

    Any sample code on how you did the header please


    Darren Rose

    Sunday, January 26, 2020 6:04 PM
  • Sadly the best option is in a free library called ObjectListView but only in C# which can be found via a search "c# objectlistview github", I know this is not useful but wanted to at least alert those reading this thread that someone actually wrote their own version of a ListView for themes and more.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, January 26, 2020 6:12 PM
    Moderator
  • Yes I had seen that, but it is 4 years old and doesn't seem to do all I required, hence asking in case a better option I had missed

    Darren Rose

    Sunday, January 26, 2020 6:20 PM
  • Hi

    Here is some code for a rather gaudy Listview. Shows one way to modify the Header cell background colour.

    ' Modified from
    ' https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.listview.ownerdraw?view=netframework-4.8
    
    Option Strict On
    Option Explicit On
    Imports System.Drawing.Drawing2D
    Imports System.Globalization
    
    Public Class Form1
      Private WithEvents listView1 As New ListView()
      Private WithEvents contextMenu1 As New ContextMenu()
      Private WithEvents listMenuItem As New MenuItem("List")
      Private WithEvents detailsMenuItem As New MenuItem("Details")
    
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With contextMenu1
          .MenuItems.Add(listMenuItem)
          .MenuItems.Add(detailsMenuItem)
        End With
        With listView1
          .BackColor = Color.Gray
          .ForeColor = Color.White
          .Dock = DockStyle.Fill
          .View = View.Details
          .FullRowSelect = True
          .OwnerDraw = True
          .ContextMenu = contextMenu1
          .Font = New Font("Arial", 14)
        End With
    
        ' Add columns to the ListView control.
        With listView1.Columns
          .Add("Name", 100, HorizontalAlignment.Center)
          .Add("First", 100, HorizontalAlignment.Center)
          .Add("Second", 100, HorizontalAlignment.Center)
          .Add("Third", 100, HorizontalAlignment.Center)
        End With
    
        ' Create items and add them to the
        ' ListView Control.
        Dim listViewItem1 As New ListViewItem(New String() {"One", "20", "30", "-40"}, -1)
        Dim listViewItem2 As New ListViewItem(New String() {"Two", "-250", "145", "37"}, -1)
        Dim listViewItem3 As New ListViewItem(New String() {"Three", "200", "800", "-1,001"}, -1)
        Dim listViewItem4 As New ListViewItem(New String() {"Four", "not available", "-2", "100"}, -1)
        Me.listView1.Items.AddRange(New ListViewItem() {listViewItem1, listViewItem2, listViewItem3, listViewItem4})
    
        ' Initialize the form and add the ListView 
        ' Control to it.
        With Me
          .ClientSize = New Size(450, 150)
          .FormBorderStyle = FormBorderStyle.FixedSingle
          .MaximizeBox = False
          .Text = "ListView OwnerDraw Example"
          .Controls.Add(listView1)
        End With
      End Sub
      ' Sets the ListView control to the List view.
      Private Sub menuItemList_Click(ByVal sender As Object, ByVal e As EventArgs) Handles listMenuItem.Click, detailsMenuItem.Click
        Dim s As String = CType(sender, MenuItem).Text
        Select Case s
          Case "List"
            listView1.View = View.List
          Case Else
            listView1.View = View.Details
        End Select
      End Sub
      ' Sets the ListView control to the Details view.
      Private Sub menuItemDetails_Click(ByVal sender As Object, ByVal e As EventArgs) Handles detailsMenuItem.Click
        listView1.View = View.Details
        ' Reset the tag on each item to re-enable the
        ' work around in the MouseMove event handler.
        For Each item As ListViewItem In listView1.Items
          item.Tag = Nothing
        Next
      End Sub
      ' Selects and focuses an item when it is
      ' clicked anywhere along its width. The
      ' click must normally be On the parent
      ' item text.
      Private Sub listView1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles listView1.MouseUp
        Dim clickedItem As ListViewItem = listView1.GetItemAt(5, e.Y)
        If (clickedItem IsNot Nothing) Then
          clickedItem.Selected = True
          clickedItem.Focused = True
        End If
      End Sub
    
      ' Draws the backgrounds for entire
      ' ListView and items.
      Private Sub listView1_DrawItem(ByVal sender As Object, ByVal e As DrawListViewItemEventArgs) Handles listView1.DrawItem
    
        If Not (e.State And ListViewItemStates.Selected) = 0 Then
          ' Draw the background for a selected item.
          e.Graphics.FillRectangle(Brushes.Blue, e.Bounds)
          e.DrawFocusRectangle()
        Else
          ' Draw the background for an unselected item.
          Dim brush As New LinearGradientBrush(e.Bounds, Color.Orange, Color.Maroon, LinearGradientMode.Horizontal)
          Try
            e.Graphics.FillRectangle(brush, e.Bounds)
          Finally
            brush.Dispose()
          End Try
        End If
    
        ' Draw the item text for views other than
        ' the Details view.
        If Not Me.listView1.View = View.Details Then
          e.DrawText()
        End If
      End Sub
    
      ' Draws subitem text and applies
      ' content-based formatting.
      Private Sub listView1_DrawSubItem(ByVal sender As Object, ByVal e As DrawListViewSubItemEventArgs) Handles listView1.DrawSubItem
    
        Dim flags As TextFormatFlags = TextFormatFlags.Left
    
        Dim sf As New StringFormat()
        Try
    
          ' Store the column text alignment, letting
          ' it default to Left if it has not been set
          ' to Center or Right.
          Select Case e.Header.TextAlign
            Case HorizontalAlignment.Center
              sf.Alignment = StringAlignment.Center
              flags = TextFormatFlags.HorizontalCenter
            Case HorizontalAlignment.Right
              sf.Alignment = StringAlignment.Far
              flags = TextFormatFlags.Right
          End Select
    
          ' Draw the text and background for a subitem
          ' With a negative value. 
          Dim subItemValue As Double
          If e.ColumnIndex > 0 AndAlso Double.TryParse(e.SubItem.Text, NumberStyles.Currency, NumberFormatInfo.CurrentInfo, subItemValue) AndAlso subItemValue < 0 Then
    
            ' Unless the item is selected, draw the
            ' standard background to make it stand
            ' out From the gradient.
            If (e.ItemState And ListViewItemStates.Selected) = 0 Then
              e.DrawBackground()
            End If
    
            ' Draw the subitem text in red to
            ' highlight it and a suitable background
            e.Graphics.FillRectangle(Brushes.Black, e.Bounds)
            e.Graphics.DrawString(e.SubItem.Text, listView1.Font, Brushes.Red, e.Bounds, sf)
            Return
          End If
    
          ' Draw normal text for a subitem with a
          ' nonnegative or nonnumerical value.
          e.DrawText(flags)
    
        Finally
          sf.Dispose()
        End Try
    
      End Sub
    
      ' Draws column headers.
      Private Sub listView1_DrawColumnHeader(ByVal sender As Object, ByVal e As DrawListViewColumnHeaderEventArgs) Handles listView1.DrawColumnHeader
    
        Dim sf As New StringFormat()
        Try
    
          ' Store the column text alignment, letting
          ' it default to Left if it has not been
          ' set to Center or Right.
          Select Case e.Header.TextAlign
            Case HorizontalAlignment.Center
              sf.Alignment = StringAlignment.Center
            Case HorizontalAlignment.Right
              sf.Alignment = StringAlignment.Far
          End Select
    
          ' Draw the modified header background.
          e.Graphics.FillRectangle(Brushes.LightGreen, e.Bounds) ' e.DrawBackground()
    
          ' Draw the header text.
          Dim headerFont As New Font("Helvetica", 10, FontStyle.Bold)
          Try
            e.Graphics.DrawString(e.Header.Text, headerFont, Brushes.Black, e.Bounds, sf)
          Finally
            headerFont.Dispose()
          End Try
    
        Finally
          sf.Dispose()
        End Try
    
      End Sub
    
      ' Forces each row to repaint itself the first
      ' time the mouse moves over it, compensating 
      ' Or an extra DrawItem event sent by the
      ' wrapped Win32 control.
      Private Sub listView1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles listView1.MouseMove
    
        Dim item As ListViewItem = listView1.GetItemAt(e.X, e.Y)
        If item IsNot Nothing AndAlso item.Tag Is Nothing Then
          listView1.Invalidate(item.Bounds)
          item.Tag = "tagged"
        End If
      End Sub
    
      ' Resets the item tags. 
      Private Sub listView1_Invalidated(ByVal sender As Object, ByVal e As InvalidateEventArgs) Handles listView1.Invalidated
    
        For Each item As ListViewItem In listView1.Items
          If item Is Nothing Then Return
          item.Tag = Nothing
        Next
      End Sub
    
      ' Forces the entire control to repaint if a
      ' column width Is changed.
      Private Sub listView1_ColumnWidthChanged(ByVal sender As Object, ByVal e As ColumnWidthChangedEventArgs) Handles listView1.ColumnWidthChanged
        listView1.Invalidate()
      End Sub
    End Class
    


    Regards Les, Livingston, Scotland

    Sunday, January 26, 2020 6:43 PM
  • Thanks Leshay, does seem that perhaps gridlines and scrollbar is not possible then?

    Darren Rose

    Sunday, January 26, 2020 7:19 PM
  • Thanks Leshay, does seem that perhaps gridlines and scrollbar is not possible then?

    Darren Rose

    Hi

    Don't know, haven't tried it.

    Check out THIS LINK for some good infp.


    Regards Les, Livingston, Scotland

    Sunday, January 26, 2020 8:38 PM
  • Thanks Leshay, does seem that perhaps gridlines and scrollbar is not possible then?


    You can draw anything over the default drawing in CDDS_ITEMPOSTPAINT, but it is weird to draw gridlines in color...

    Sunday, January 26, 2020 8:52 PM
  • Thanks Leshay, does seem that perhaps gridlines and scrollbar is not possible then?



    You can draw anything over the default drawing in CDDS_ITEMPOSTPAINT, but it is weird to draw gridlines in color...

    How?

    Reason is because skin of my app is dark grey, so grid lines look too bright if white, so want them a light gray like other components I use


    Darren Rose

    Sunday, January 26, 2020 8:54 PM
  • Appreciate all your responses, in the end I decided to rewrite my app to use DevExpress GridControl instead of standard ListView so that I could take advantage of colour schemes / skinning etc rather than having just one control looking so out of place!

    Thanks again


    Darren Rose

    Monday, January 27, 2020 6:09 PM
  • Appreciate all your responses, in the end I decided to rewrite my app to use DevExpress GridControl instead of standard ListView so that I could take advantage of colour schemes / skinning etc rather than having just one control looking so out of place!

    Thanks again


    Darren Rose

    Smart move, I never like to bring up third party stuff but I use telerik which allows theming too.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, January 27, 2020 8:57 PM
    Moderator