Results 1 to 12 of 12

Thread: [RESOLVED] sorted fileinfo compared to listview sort

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Resolved [RESOLVED] sorted fileinfo compared to listview sort

    I have this function that gets the files in a specific folder to be added to the listview. That all works, but I am looking to sort the files before they are added to the listview. The problem is if I sort the listview in properties and then add the files to the list, the listview item tags are not set, they are as how they get added to the listview.

    I have search and found LINQ but when I try it sorts are different than the lsitview property sort

    Code:
    Public Function ListFoldersFiles(ByVal path As String, ByVal lvTemp As ListView, ByVal imgLtemp As ImageList) As String()
    
            ' Create a reference to the current directory.
            Dim di As New DirectoryInfo(path)
            'MsgBox(di.ToString)
            If Directory.Exists(di.ToString) Then
    
                ' Create an array representing the files in the current directory.
                Dim fi As FileInfo() = di.GetFiles()
                Dim fiTemp As FileInfo
                Dim i As Short
                Array.Sort(fi, New compclass(SortOrder.Ascending))
    
                lvTemp.Items.Clear()
    
                i = 0
    
                ' Loop through each file in the directory
                For Each fiTemp In fi
    
                    Dim strImageKey As String = String.Empty
                    Try
                        ' gets the icon from file
                        Dim ico As Icon = Icon.ExtractAssociatedIcon(path & fiTemp.Name)
                        If ico IsNot Nothing Then
                            Dim bmp As Bitmap = ico.ToBitmap()
                            strImageKey = bmp.GetHashCode.ToString
                            imgLtemp.Images.Add(strImageKey, bmp)
                        End If
                    Catch ex As Exception
    
                    End Try
    
                    ' split the extension off so we can use just the text.
    
                    Dim rFileName As Array = Split(fiTemp.Name, ".")
                    ' MsgBox(rFileName(0))
                    lvTemp.Items.Add(fiTemp.Name, rFileName(0), strImageKey)
                    ' add full name to tag so we can call it later seeing how we 
                    ' don't use the extension for the text.
    
                    lvTemp.Items(i).Tag = path & fiTemp.Name
                    ' MsgBox(lvTemp.Items(i).Tag)
    
                    i = i + 1
    
                Next fiTemp
            End If
        End Function

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: sorted fileinfo compared to listview sort

    I'm not really sure what you're saying your problem is. If you set the Tag of a ListViewItem then the Tag of that item doesn't change just because you sort the items. As you alluded to, LINQ is basically your simplest option here, e.g.
    vb.net Code:
    1. Dim folder As New IO.DirectoryInfo("file path here")
    2.  
    3. For Each file As IO.FileInfo In folder.GetFiles().OrderBy(Function(fi) fi.CreationTime)
    4.     Dim item As New ListViewItem(file.Name) With {.Tag = file}
    5.  
    6.     Me.ListView1.Items.Add(item)
    7. Next
    That will give you your initial order but you may still want to consider allowing the user to sort the list by clicking on a column header.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: sorted fileinfo compared to listview sort

    When I click on the treeview child, it loads the icons correctly. but when I go and click on the icon it's tag is empty. I checked that each tag is added when the icons are added to the listview and they were correct. But when actually clicking on the icon it is wrong. I took off the sort property on the listview and it worked just like it should, all items in the child had the correct tag info.

    Now, when I added the above code to sort on the fileinfo it is not the same as the listview sort. I would rather have the listview sort as it is more alphabetically correct, the sort with the fileinfo is not correct. I sort by name, not date. So when I used LINQ the .Name is not an option here "fi.CreationTime" It gives me errors.

    for instance, if I have sort on the listview in the properties section, and I have 4 icons listed in the listview, I click on the last icon listed (large icon view) I get the first icons tag, yet the first icon's tag is empty. So yes it does change and it is all related to the sort property on the lsitview.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: sorted fileinfo compared to listview sort

    I need to bring this backup as I am still having issues. Again, if I do not sort on anything it works fine, but once you sort the listview on large icons it loses the tag indexing. I do not use columns and we will not allow the user to sort. so sorting ascending is preferred.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: sorted fileinfo compared to listview sort

    this works:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         Dim folder As New IO.DirectoryInfo("folder path")
    5.  
    6.         For Each file As IO.FileInfo In folder.GetFiles().OrderBy(Function(fi) fi.CreationTime)
    7.             Dim strImageKey As String = String.Empty
    8.             Try
    9.                 ' gets the icon from file
    10.                 Dim ico As Icon = Icon.ExtractAssociatedIcon(file.FullName)
    11.                 If ico IsNot Nothing Then
    12.                     Dim bmp As Bitmap = ico.ToBitmap()
    13.                     strImageKey = bmp.GetHashCode.ToString
    14.                     imgLtemp.Images.Add(strImageKey, bmp)
    15.                 End If
    16.  
    17.                 Dim item As New ListViewItem(file.Name, imgLtemp.Images.Count - 1) With {.Tag = file}
    18.                 Me.ListView1.Items.Add(item)
    19.  
    20.             Catch ex As Exception
    21.  
    22.             End Try
    23.         Next
    24.     End Sub
    25.  
    26. End Class

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: sorted fileinfo compared to listview sort

    But that sorts on creationdate, I need them sorted alphabetically by name.

    Plus when I try that code I get an error on this line

    folder.GetFiles().OrderBy

    Stating that "OderBy" is not a member of "system.array".

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: sorted fileinfo compared to listview sort

    which version of vb.net are you using?

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: sorted fileinfo compared to listview sort

    2010 express

  9. #9
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    Re: sorted fileinfo compared to listview sort

    i would use:

    vb Code:
    1. ListView1.Items.AddRange((From xItem In FileIO.FileSystem.GetFiles("c:\", FileIO.SearchOption.SearchTopLevelOnly, "*.*") Order By xItem Select New ListViewItem(IO.Path.GetFileName(xItem)) With {.Tag = xItem}).ToArray)

    Kris

  10. #10
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    Re: sorted fileinfo compared to listview sort

    Also ... maybe you should list the item sort from both examples

    for eg:
    Listbox sort order:
    aa
    a
    b
    Linq sort order:
    a
    aa
    b

    Kris

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: sorted fileinfo compared to listview sort

    which framework are you targetting?

    is it an upgraded project?

    be sure you have a reference to System.Xml.Linq

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: sorted fileinfo compared to listview sort

    I think I got it with a modification from pauls code

    so far this is working
    Code:
     Public Sub ListFoldersFiles(ByVal path As String, ByVal lvTemp As ListView, ByVal imgLtemp As ImageList)
           
            ' Create a reference to the current directory.
            Dim di As New DirectoryInfo(path)
            
            If Directory.Exists(di.ToString) Then
    
                ' Create an array representing the files in the current directory.
                Dim fi As FileInfo() = di.GetFiles()
                Dim fiTemp As FileInfo
                Dim i As Short
                Array.Sort(fi, New compclass(SortOrder.Ascending))
    
                lvTemp.Items.Clear()
               
                           
                ' Loop through each file in the directory
                For Each fiTemp In fi
    
                    Dim strImageKey As String = String.Empty
                    Try
                        ' gets the icon from file
                        Dim ico As Icon = Icon.ExtractAssociatedIcon(path & fiTemp.Name)
                        If ico IsNot Nothing Then
                            Dim bmp As Bitmap = ico.ToBitmap()
                            strImageKey = bmp.GetHashCode.ToString
                            imgLtemp.Images.Add(strImageKey, bmp)
                        End If
                    Catch ex As Exception
    
                    End Try
    
                    ' split the extension off so we can use just the text.
    
                    Dim rFileName As Array = Split(fiTemp.Name, ".")                
                    Dim fname As String = rFileName(0)
                   
                    ' add full name to tag so we can call it later seeing how we 
                    ' don't use the extension for the text.
                    Dim item As New ListViewItem(fname, imgLtemp.Images.Count - 1) With {.Tag = path & fiTemp.Name}
                    lvTemp.Items.Add(item)
                                 
    
                Next fiTemp
    
            End If
        End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width