Listview sort by backcolor
If you need help with a project or need to know how to do something specific in VB.NET then please ask your questions in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
5 posts
Page 1 of 1
I want to sort the listview by difrent backcolor so ex green is at top and red at bottom
You can use this to sort the green/red background items:
Code: Select all
Public Class Form1
Private Sub btnSort_Click(sender As System.Object, e As System.EventArgs) Handles btnSort.Click
ListView1.ListViewItemSorter = New checkBackground()
End Sub
End Class
Class checkBackground : Implements IComparer
Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
Dim lx As ListViewItem = x
Dim ly As ListViewItem = y
If lx.BackColor = Color.Green Then
Return 0
ElseIf lx.BackColor = Color.Red Then
Return 1
End If
End Function
End Class
Thank you!
Last edited by AnoPem on Mon Jun 04, 2012 10:24 am, edited 1 time in total.
You shouldn't need to add any references. Try to import System.Collections and see if that helps.
If you want to sort by brightness, you could replace the "Compare" function in mandai's code with this:
If you want to sort by brightness, you could replace the "Compare" function in mandai's code with this:
Code: Select all
Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
Dim lx As ListViewItem = x
Dim ly As ListViewItem = y
If lx.BackColor.GetBrightness() > ly.BackColor.GetBrightness() Then
Return 1
Else
Return -1
End If
End Function
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
![Image]()
![Image]()
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!

MrAksel wrote:You shouldn't need to add any references. Try to import System.Collections and see if that helps.I didnt read the code good enough first time
If you want to sort by brightness, you could replace the "Compare" function in mandai's code with this:Code: Select allFunction Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare Dim lx As ListViewItem = x Dim ly As ListViewItem = y If lx.BackColor.GetBrightness() > ly.BackColor.GetBrightness() Then Return 1 Else Return -1 End If End Function

5 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023