Yay guys, it's me again.

I am using the code from here: http://www.daniweb.com/software-development/vbnet/code/370426 to sort my listview items. But I want something more 'dynamic': when I click on a header column, I want that it changes the sorting based on the column that has been clicked. here is my code:

'Classes usadas
Imports System.Math
Imports System.IO
Imports System.Threading.Thread

Public Class Main
    'Define as variáveis
    Dim CaminhoArquivo As String
    Private Sub ProcuraDir(sender As System.Object, e As System.EventArgs) Handles btnProcuraPasta.Click
        'Exibe o diálogo de procura
        If ProcuraPasta.ShowDialog = DialogResult.OK Then
            'Mostra o nome do arquivo na caixa de texto associada
            Caminho.Text = ProcuraPasta.SelectedPath
            CaminhoArquivo = ProcuraPasta.SelectedPath

            For Each myCoolFile As String In My.Computer.FileSystem.GetFiles(CaminhoArquivo, FileIO.SearchOption.SearchTopLevelOnly, "*.*")
                Dim Arquivo As New FileInfo(myCoolFile)
                Dim Tamanho As Decimal = Arquivo.Length
                Dim TamanhoDividido As Decimal = 0
                Dim TamanhoReal As String = ""
                Dim ItemLista As New ListViewItem(Path.GetFileName(myCoolFile))

                If Tamanho > 1073741824 Then
                    TamanhoDividido = Tamanho / 1073741824
                    TamanhoReal = Round(TamanhoDividido, 2) & "GB"
                ElseIf Tamanho > 1048576 And Tamanho < 1073741824 Then
                    TamanhoDividido = Tamanho / 1048576
                    TamanhoReal = Round(TamanhoDividido, 2) & "MB"
                ElseIf Tamanho > 1024 And Tamanho < 1048576 Then
                    TamanhoDividido = Tamanho / 1024
                    TamanhoReal = Round(TamanhoDividido, 2) & "KB"
                ElseIf Tamanho < 1024 Then
                    TamanhoReal = Tamanho & " bytes"
                End If

                ItemLista.SubItems.Add(TamanhoReal)
                ItemLista.SubItems.Add(Path.GetExtension(myCoolFile))
                ItemLista.SubItems.Add("rwhsa")
                ItemLista.SubItems.Add(Path.GetFullPath(myCoolFile))
                ListView1.Items.Add(ItemLista)
            Next
        End If
        GroupListView(ListView1, 2)
    End Sub
    Private Sub btnProcessar_Click(sender As System.Object, e As System.EventArgs) Handles btnProcessar.Click
        For Each s As ListViewItem In ListView1.SelectedItems
            MsgBox("Item selecionado: " & s.Text)
        Next
    End Sub

    Public Sub GroupListView(ByVal lstV As ListView, ByVal SubItemIndex As Int16)
        Dim flag As Boolean = True
        For Each l As ListViewItem In lstV.Items

            Dim strmyGroupname As String = l.SubItems(SubItemIndex).Text

            For Each lvg As ListViewGroup In lstV.Groups

                If lvg.Name = strmyGroupname Then
                    l.Group = lvg
                    flag = False
                End If

            Next

            If flag = True Then
                Dim lstGrp As New ListViewGroup(strmyGroupname, strmyGroupname)
                lstV.Groups.Add(lstGrp)
                l.Group = lstGrp
            End If

            flag = True

        Next

    End Sub
End Class

This is pretty much the last thing that I don't know how to do that I want to do :D And please ignore the code on the sub btnProcessar_Click , it is useless by now!

Thanks!

I made this for someone back in March and it works with 4 Columns in a ListView.
1 ListView needed for testing.

Option Strict On
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        With ListView1
            .Columns.Add("FirstName", 100) : .Columns.Add("LastName", 100) : .Columns.Add("Age", 100) : .Columns.Add("In Class?", 100) : .View = View.Details : .CheckBoxes = True : .FullRowSelect = True
            With .Items
                Dim lvItm As New ListViewItem("xnew,subitem,23,Yes".Split(CChar(",")))
                .Add(lvItm)
                lvItm = New ListViewItem("a 1new,1subitem,23,No".Split(CChar(",")))
                .Add(lvItm)
                lvItm = New ListViewItem("b 2new,2subitem,23,Yes".Split(CChar(",")))
                .Add(lvItm)
                lvItm = New ListViewItem("c 2new,1subitem,43,No".Split(CChar(",")))
                .Add(lvItm)
                lvItm = New ListViewItem("a 2new,2subitem,43,Yes".Split(CChar(",")))
                .Add(lvItm)
            End With
        End With
    End Sub

    Private Sub ListView1_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles ListView1.ColumnClick
        '// check if it contains items.
        If Not ListView1.Items.Count = 0 Then lvColumnSort(e.Column, ListView1) '// send Column Index and the ListView used.
    End Sub

#Region "================= SORT LISTVIEW COLUMNS BY SELECTED COLUMN ================="

    Private arlSortLvItems As New ArrayList '// adds ListView items to.
    Private bSortDescending As Boolean = True '// Toggles if .Ascending or Descending.

    Private Sub lvColumnSort(ByVal columnIndex As Integer, ByVal selectedListView As ListView)
        With selectedListView
            arlSortLvItems.Clear() '// clear ArrayList for new input.
            For Each itm As ListViewItem In .Items '// loop thru ListView.Items
                Select Case columnIndex '// check which column is clicked and add items to ArrayList as needed, first item will be the .Sorting item.
                    Case 0 '// FirstName, LastName, Age, InClass.
                        arlSortLvItems.Add(itm.Text & "~" & itm.SubItems(1).Text & "~" & itm.SubItems(2).Text & "~" & itm.SubItems(3).Text)
                    Case 1 '// LastName, FirstName, Age, InClass.
                        arlSortLvItems.Add(itm.SubItems(1).Text & "~" & itm.Text & "~" & itm.SubItems(2).Text & "~" & itm.SubItems(3).Text)
                    Case 2 '// Age, InClass, FirstName, LastName.
                        arlSortLvItems.Add(itm.SubItems(2).Text & "~" & itm.SubItems(3).Text & "~" & itm.Text & "~" & itm.SubItems(1).Text)
                    Case 3 '// InClass, Age, FirstName, LastName.
                        arlSortLvItems.Add(itm.SubItems(3).Text & "~" & itm.SubItems(2).Text & "~" & itm.Text & "~" & itm.SubItems(1).Text)
                End Select
            Next
            arlSortLvItems.Sort() '// sort ArrayList.
            .Items.Clear() '// clear ListView to add Sorted items back.
            getSortOrderOfItems(columnIndex, selectedListView, bSortDescending) '// determines if needed to add .Ascending or .Descending.
            bSortDescending = Not bSortDescending '// Toggle True/False for next click.
        End With
    End Sub

    Private Sub getSortOrderOfItems(ByVal columnIndex As Integer, ByVal selectedListView As ListView, ByVal isDescending As Boolean)
        With selectedListView
            .Sorting = SortOrder.None '// No Sort order.
            If isDescending Then
                For Each itm As String In arlSortLvItems '// loop thru items in ArrayList.
                    setItemsBackToListView(columnIndex, selectedListView, itm.Split("~"c)) '// .Split to get values as arrays.
                Next
            Else
                For i As Integer = arlSortLvItems.Count - 1 To 0 Step -1 '// loop backwards thru items ArrayList.
                    setItemsBackToListView(columnIndex, selectedListView, arlSortLvItems(i).ToString.Split("~"c)) '// .Split to get values as arrays.
                Next
            End If
        End With
    End Sub

    Private Sub setItemsBackToListView(ByVal columnIndex As Integer, ByVal selectedListView As ListView, ByVal ArrayListItem() As String)
        With selectedListView
            Dim lvItem As New ListViewItem '// new ListView Item.
            With lvItem
                Select Case columnIndex
                    Case 0 '// FirstName, LastName, Age, InClass.
                        .Text = ArrayListItem(0) '// FirstName.
                        .SubItems.Add(ArrayListItem(1)) '// LastName.
                        .SubItems.Add(ArrayListItem(2)) '// Age.
                        .SubItems.Add(ArrayListItem(3)) '// InClass.
                    Case 1 '// LastName, FirstName, Age, InClass.
                        .Text = ArrayListItem(1) '// FirstName.
                        .SubItems.Add(ArrayListItem(0)) '// LastName.
                        .SubItems.Add(ArrayListItem(2)) '// Age.
                        .SubItems.Add(ArrayListItem(3)) '// InClass.
                    Case 2 '// Age, InClass, FirstName, LastName.
                        .Text = ArrayListItem(2) '// FirstName.
                        .SubItems.Add(ArrayListItem(3)) '// LastName.
                        .SubItems.Add(ArrayListItem(0)) '// Age.
                        .SubItems.Add(ArrayListItem(1)) '// InClass.
                    Case 3 '// InClass, Age, FirstName, LastName.
                        .Text = ArrayListItem(2) '// FirstName.
                        .SubItems.Add(ArrayListItem(3)) '// LastName.
                        .SubItems.Add(ArrayListItem(1)) '// Age.
                        .SubItems.Add(ArrayListItem(0)) '// InClass.
                End Select
            End With
            .Items.Add(lvItem) '// add item back to ListView.
        End With
    End Sub
#End Region

End Class

Edited 5 Years Ago by codeorder: n/a

Thanks codeorder, I just have to study your code a bit to implement it into my app (:

Can you just tell me what that Option Strict On does? Thanks again!

This question has already been answered. Start a new discussion instead.