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.
5 posts Page 1 of 1
Contributors
User avatar
AnoPem
VIP - Donator
VIP - Donator
Posts: 441
Joined: Sat Jul 24, 2010 10:55 pm

Listview sort by backcolor
AnoPem
I want to sort the listview by difrent backcolor so ex green is at top and red at bottom
https://t.me/pump_upp
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Listview sort by backcolor
mandai
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
User avatar
AnoPem
VIP - Donator
VIP - Donator
Posts: 441
Joined: Sat Jul 24, 2010 10:55 pm

Re: Listview sort by backcolor
AnoPem
Thank you!
Last edited by AnoPem on Mon Jun 04, 2012 10:24 am, edited 1 time in total.
https://t.me/pump_upp
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Re: Listview sort by backcolor
MrAksel
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:
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
User avatar
AnoPem
VIP - Donator
VIP - Donator
Posts: 441
Joined: Sat Jul 24, 2010 10:55 pm

Re: Listview sort by backcolor
AnoPem
MrAksel wrote:
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:
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
I didnt read the code good enough first time :) i figured it out, thanks anyway
https://t.me/pump_upp
5 posts Page 1 of 1
Return to “Coding Help & Support”