0

ok im going to try to explain this the best i can.

So i have a list viewer for image thumbnails. I need to be able to click a button and it load the full dir. The issue is the normal way loads the images in but they are out of order. 1.jpg 10.jpg 100.jpg

        For Each file As String In My.Computer.FileSystem.GetFiles(appPath + "\" + ConfigurationManager.AppSettings("activedisplay").ToString + "\" + Bfolder.Text + "\")
        ImageListView1.Items.Add(file)
    Next

So i went and looked around for a filter

        Dim files = Directory.EnumerateFiles(appPath + "\" + ConfigurationManager.AppSettings("activedisplay").ToString + "\" + Bfolder.Text + "\").
                     Select(Function(s) Path.GetFileName(s)).ToList
    Console.WriteLine("Before: {0}", String.Join(", ", files))

    ' sort the list using the Natural Comparer:
    files.Sort(myComparer)
MsgBox((String.Join(", ", files)))

So this script puts them in the right order 1,2,3,4,5.. but i cant figure out how to open it this way. Cause

ImageListViewer1.items.addrange((String.Join(", ", files)))

overloads. Like openFileDialog.FileNames has the ability to open multiple files at once so I know its possible but i don't want to use a dialog. To the point i need a way to load the file string produced by this which is 1.jpg,2.jpg,3.jpg into the ImageViewerList.

This creates a string of the file in the correct order "(String.Join(", ", files))" is there a way i can load files from a string.

For Each file As String In My.Computer.FileSystem.GetFiles(appPath, string

and it be able to load the string of files it creates the string like so 1.jpg,2.jpg,3.jpg continued for all files in the dir it looked at

iv looked around google for assistance and looked at the ms page on getfiles but i have had no luck. any ways any help would be greatly appreciated thanks in advance -Fox

Fox243
  • 7
  • 3
  • If you use `Option Strict On` then Visual Studio will be able to show you where the code has problems with variable type mismatches. – Andrew Morton Mar 12 '17 at 19:22

1 Answers1

0

You need to write your own sort function to sort the numeric values. I referred the link, for CustomSort function. Below code sorts the file names and works nicely for me.

Dim Dir As String = appPath + "\" + ConfigurationManager.AppSettings("activedisplay").ToString + "\" + Bfolder.Text + "\"
Dim fileList = New DirectoryInfo(Dir).GetFiles("*.jpg").[Select](Function(o) o.Name).ToList()
Dim sortedList = CustomSort(fileList).ToList()


Public Shared Function CustomSort(list As IEnumerable(Of String)) As IEnumerable(Of String)
    Dim maxLen As Integer = list.[Select](Function(s) s.Length).Max()

    Return list.[Select](Function(s) New With { _
        Key .OrgStr = s, _
        Key .SortStr = Regex.Replace(s, "(\d+)|(\D+)", Function(m) m.Value.PadLeft(maxLen, If(Char.IsDigit(m.Value(0)), " "c, "?"c))) _
    }).OrderBy(Function(x) x.SortStr).[Select](Function(x) x.OrgStr)
End Function
Community
  • 1
  • 1
Mukul Varshney
  • 3,131
  • 1
  • 12
  • 19
  • i just want to let you know that a few changes to this made it work perfectly. thank you very much. for your time and knowledge of a solution. saved me many hours of stress. is there ability to private msg on here? – Fox243 Mar 13 '17 at 15:45
  • i don't know about pvt msgs. If you come across, do let me know. – Mukul Varshney Mar 13 '17 at 16:08