vb.net combo box
-
WHat Richard MacCutchan said - but personally I wouldn't bother with any "manual" sorting method / algorithm. Instead, add a listbox, set its Visible property to false, and Sort = True. Then load the items into that and let tt take of sorting them. Then, to get reverse order, just read the items in reverse and load them into your combo box. :)
Private Function reverseFileNameComparer() As Comparison(Of String)
Return Function(x, y) (String.Compare(Path.GetFileNameWithoutExtension(y), Path.GetFileNameWithoutExtension(x), StringComparison.OrdinalIgnoreCase))
End FunctionPrivate Sub AddToCombo(combo As ComboBox, directoryPath As String, filter As String)
' Error check, 'IsNothing(combo)' and 'IO.Directory.Exists(directoryPath)' bla-bla
combo.BeginUpdate()
For Each fn As String In Directory.EnumerateFiles(directoryPath).OrderByDescending(Function(f) (Path.GetFileNameWithoutExtension(f)))
combo.Items.Add(Path.GetFileNameWithoutExtension(fn))
Next
combo.EndUpdate()' Or... Dim files() As String = Directory.GetFiles(directoryPath, filter) Array.Sort(files, reverseFileNameComparer) combo.Items.AddRange(Array.ConvertAll(files, AddressOf Path.GetFileNameWithoutExtension)) 'Since you want to order by file name, I suggest you use the System.IO namespace: 'You could: combo.FormattingEnabled = True 'Add a handler to 'ComboBox.Format - 'or -- combo.DisplayMember = "Name" combo.BeginUpdate() For Each fio As FileInfo In New DirectoryInfo(directoryPath).EnumerateFiles(filter).OrderByDescending(Function(f) (f.Name)) combo.Items.Add(fio.Name) Next combo.EndUpdate()
End Sub
Private Sub cboFileInfo_format(sender As Object, e As ListControlConvertEventArgs)
Dim f As FileInfo = DirectCast(e.ListItem, FileInfo)
e.Value = f.Name.Substring(f.Name.LastIndexOf("."c) + 1)
End Sub