vb.net combo box
-
Bah - only to purists! :) Technically, you are quite right of course, but I suspect Linq is a bit beyond our OP at present. Then it depends on one's motivation; there's nothing wrong with "cheating" if getting a working result is what matters. Even if this isn't the best place to use it, this "cheat" is a useful way of sorting items without resorting to complex methods, which our OP could use elsewhere. But without a knowledge of Linq, your answer would be used "by rote" and the OP would learn nothing - other, perhaps, than that maybe he should learn about Linq. I am not really trying to say I'm right and you're not - of course you are; just that there can be a place for the quick (because it doesn't require learning new methods) and dirty solutions sometimes.
Wombaticus wrote:
but I suspect Linq is a bit beyond our OP at present
You're probably right there but it can also be done without Linq:
Dim dirAccessFiles As String() = Directory.GetFiles("H:\FilesTest", "*.accdb")
Dim list As New List(Of String)()
For Each dir As String In dirAccessFiles
list.Add(Path.GetFileNameWithoutExtension(dir))
Next
list.Sort()
For i As Integer = list.Count - 1 To 0 Step -1
comboBox1.Items.Add(list(i))
NextIf the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
-
Wombaticus wrote:
but I suspect Linq is a bit beyond our OP at present
You're probably right there but it can also be done without Linq:
Dim dirAccessFiles As String() = Directory.GetFiles("H:\FilesTest", "*.accdb")
Dim list As New List(Of String)()
For Each dir As String In dirAccessFiles
list.Add(Path.GetFileNameWithoutExtension(dir))
Next
list.Sort()
For i As Integer = list.Count - 1 To 0 Step -1
comboBox1.Items.Add(list(i))
NextIf the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
This is true - and I have to admit that this obvious solution only occurred to me belatedly. :-O Still, my "trick" has many applications! (I'm not giving up on it that easily! :) )
-
This is true - and I have to admit that this obvious solution only occurred to me belatedly. :-O Still, my "trick" has many applications! (I'm not giving up on it that easily! :) )
Wombaticus wrote:
Still, my "trick" has many applications! (I'm not giving up on it that easily! :) )
:-D I didn't yet have to resort to some such trick - do you have an example where it can't be easily replaced with some non-control-using code?
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
-
Wombaticus wrote:
Still, my "trick" has many applications! (I'm not giving up on it that easily! :) )
:-D I didn't yet have to resort to some such trick - do you have an example where it can't be easily replaced with some non-control-using code?
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
Well, it could be used in many places in lieu of a sorting algorithm. If what you’re saying is anything that can be read into a ListBox and sorted that way could be read into a List collection, ok fair enough. I guess this old trick goes back before the days of such things... (If they existed in VB3 I wasn’t aware of them!) I don’t really want to labour the point – it is a “dirty” trick, so probably shouldn’t be encouraged, but it’s quick and as easy to implement as a “proper” solution. Otherwise, OK, I give in! </grovels>
-
Well, it could be used in many places in lieu of a sorting algorithm. If what you’re saying is anything that can be read into a ListBox and sorted that way could be read into a List collection, ok fair enough. I guess this old trick goes back before the days of such things... (If they existed in VB3 I wasn’t aware of them!) I don’t really want to labour the point – it is a “dirty” trick, so probably shouldn’t be encouraged, but it’s quick and as easy to implement as a “proper” solution. Otherwise, OK, I give in! </grovels>
Wombaticus wrote:
I guess this old trick goes back before the days of such things...
Fair enough.. old habits ;)
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
-
Wombaticus wrote:
Still, my "trick" has many applications! (I'm not giving up on it that easily! :) )
:-D I didn't yet have to resort to some such trick - do you have an example where it can't be easily replaced with some non-control-using code?
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
-
What Richard said. Not what Wombaticus said. Sorting is easy. This replaces your posted code except the try-catch-block:
Dim dirAccessFiles As String() = Directory.GetFiles("H:\FilesTest", "*.accdb") _
.Select(Function(x) Path.GetFileNameWithoutExtension(x)) _
.OrderByDescending(Function(x) x) _
.ToArray()cboAccessFile.Items.AddRange(dirAccessFiles)
(You'll need an "Imports System.Linq" at the top of the file.)
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
You might want to pass
StringComparer.OrdinalIgnoreCase
to theOrderByDescending
function, since file names in Windows aren't case-sensitive. :) If you want to mimic the "natural" sort order that Windows Explorer uses, you'll need a different comparer - for example: Numeric String Sort in C#[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
You might want to pass
StringComparer.OrdinalIgnoreCase
to theOrderByDescending
function, since file names in Windows aren't case-sensitive. :) If you want to mimic the "natural" sort order that Windows Explorer uses, you'll need a different comparer - for example: Numeric String Sort in C#[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Good suggestion; also the linked article, thank you! :)
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
-
This is true - and I have to admit that this obvious solution only occurred to me belatedly. :-O Still, my "trick" has many applications! (I'm not giving up on it that easily! :) )
That's a trick I used many times in VB3, 5 and 6 (and some obscure VB derivatives that I'd rather forget about). I agree with the others that there are (arguably) better "dotnetty" ways of doing it, but it's always worth having something like this up your sleeve (especially if you end up supporting any legacy systems) The only thing I would say is that (when possible) I created the listbox dynamically in code only so it was never part of the forms control collection.
-
Wombaticus wrote:
but I suspect Linq is a bit beyond our OP at present
You're probably right there but it can also be done without Linq:
Dim dirAccessFiles As String() = Directory.GetFiles("H:\FilesTest", "*.accdb")
Dim list As New List(Of String)()
For Each dir As String In dirAccessFiles
list.Add(Path.GetFileNameWithoutExtension(dir))
Next
list.Sort()
For i As Integer = list.Count - 1 To 0 Step -1
comboBox1.Items.Add(list(i))
NextIf the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
Thanks for your helpful answers!
-
Thanks for your helpful answers!
You're welcome!
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
-
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