|
-
Feb 23rd, 2011, 01:33 PM
#1
Thread Starter
Frenzied Member
[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
-
Feb 23rd, 2011, 06:08 PM
#2
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:
Dim folder As New IO.DirectoryInfo("file path here")
For Each file As IO.FileInfo In folder.GetFiles().OrderBy(Function(fi) fi.CreationTime)
Dim item As New ListViewItem(file.Name) With {.Tag = file}
Me.ListView1.Items.Add(item)
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.
-
Feb 23rd, 2011, 07:45 PM
#3
Thread Starter
Frenzied Member
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.
-
Oct 4th, 2011, 04:59 PM
#4
Thread Starter
Frenzied Member
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.
-
Oct 4th, 2011, 05:11 PM
#5
Re: sorted fileinfo compared to listview sort
this works:
vb Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim folder As New IO.DirectoryInfo("folder path")
For Each file As IO.FileInfo In folder.GetFiles().OrderBy(Function(fi) fi.CreationTime)
Dim strImageKey As String = String.Empty
Try
' gets the icon from file
Dim ico As Icon = Icon.ExtractAssociatedIcon(file.FullName)
If ico IsNot Nothing Then
Dim bmp As Bitmap = ico.ToBitmap()
strImageKey = bmp.GetHashCode.ToString
imgLtemp.Images.Add(strImageKey, bmp)
End If
Dim item As New ListViewItem(file.Name, imgLtemp.Images.Count - 1) With {.Tag = file}
Me.ListView1.Items.Add(item)
Catch ex As Exception
End Try
Next
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 4th, 2011, 05:39 PM
#6
Thread Starter
Frenzied Member
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".
-
Oct 4th, 2011, 05:44 PM
#7
Re: sorted fileinfo compared to listview sort
which version of vb.net are you using?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 4th, 2011, 05:45 PM
#8
Thread Starter
Frenzied Member
Re: sorted fileinfo compared to listview sort
-
Oct 4th, 2011, 05:45 PM
#9
Re: sorted fileinfo compared to listview sort
i would use:
vb Code:
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
-
Oct 4th, 2011, 05:50 PM
#10
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
-
Oct 4th, 2011, 05:56 PM
#11
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 4th, 2011, 05:57 PM
#12
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|