Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 919
  • Last Modified:

Change ListView Highlight Color

I would like to be able to control the highlight color of each listview item individually depending on what their individual backcolors are. I found a way to change all of them on the form but that is not what I want. Basically, if the backcolor is a light red, I want the highlight color to be a dark red. Is this possible? Thanks.
0
rbichon
Asked:
rbichon
  • 2
1 Solution
 
David L. HansenProgrammer AnalystCommented:
Try this:

Private Sub listBox1_DrawItem(sender As Object, e As DrawItemEventArgs)
	Dim index As Integer = e.Index
	Dim g As Graphics = e.Graphics
	For Each selectedIndex As Integer In Me.listBox1.SelectedIndices
		If index = selectedIndex Then
			' Draw the new background color
			e.DrawBackground()
			g.FillRectangle(New SolidBrush(Color.Red), e.Bounds)
		End If
	Next

	' Get the item details
	Dim font As Font = listBox1.Font
	Dim colour As Color = listBox1.ForeColor
	Dim text As String = listBox1.Items(index).ToString()

	' Print the text
	g.DrawString(text, font, New SolidBrush(Color.Black), CSng(e.Bounds.X), CSng(e.Bounds.Y))
	e.DrawFocusRectangle()

End Sub

Private Sub listBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
	listBox1.Invalidate()
End Sub

Open in new window

0
 
rbichonAuthor Commented:
I had to modify some of the code to fit my needs, but this was exactly what I was looking for. Thanks!
0
 
David L. HansenProgrammer AnalystCommented:
Glad to help. :)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now