|
Wrong approach!
Store dates as dates, not string! String represenation may differ in different localization/culture.
Always initialize variables! It can save you from stress
|
|
|
|
|
In a VB.NET 2010 desktop application, I am using the following new logic to load values into a combo box.
Public dirAccessFiles As String() = Directory.GetFiles("H:\FilesTest", "*.accdb")
Try
Dim dir As String
For Each dir In dirAccessFiles
cboAccessFile.Items.Add(Path.GetFileNameWithoutExtension(dir))
Next
Catch except As Exception
Console.WriteLine("The process failed: {0}", e.ToString())
End Try
The user wants the file names to be in descending order in the combo box. Thus, can you tell me how to modify the code list above and/or would you show me code on how I can load file names into the combo box in descending order?
|
|
|
|
|
That means you need to set the Sorted property to False , and sort the names manually before adding them to the Combo.
|
|
|
|
|
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.
|
|
|
|
|
Would you show me how to write the code you suggested here," 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."? Could you give me at least a start on how to set this up?
|
|
|
|
|
Well...
Add ListBox1 to form
set
ListBox1.Visible = False
ListBox1.Sorted = True
Public dirAccessFiles As String() = Directory.GetFiles("H:\FilesTest", "*.accdb")
...
For Each dir In dirAccessFiles
ListBox1.Items.Add(Path.GetFileNameWithoutExtension(dir))
Next
Dim I as integer
For I = ListBox1.Items.Count - 1 To 0 Step -1
cboAccessFile.Items.Add(ListBox1.Items(I))
Next
|
|
|
|
|
That's a toe-curling suggestion.. please see my answer below
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
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))
Next
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. 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! )
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>
|
|
|
|
|
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
|
|
|
|
|
It's a very useful tip when creating a TreeView which lists the items in sorted order. You can also add a prefix character to each item so that directories appear before files.
|
|
|
|
|
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.
|
|
|
|
|
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
|
|
|
|
|
Yes, that is probably the way I would have done it. However, given the tenor of the question I doubt that the OP could do it without a full sample of code.
|
|
|
|
|
Private Function reverseFileNameComparer() As Comparison(Of String)
Return Function(x, y) (String.Compare(Path.GetFileNameWithoutExtension(y), Path.GetFileNameWithoutExtension(x), StringComparison.OrdinalIgnoreCase))
End Function
Private Sub AddToCombo(combo As ComboBox, directoryPath As String, filter As String)
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()
Dim files() As String = Directory.GetFiles(directoryPath, filter)
Array.Sort(files, reverseFileNameComparer)
combo.Items.AddRange(Array.ConvertAll(files, AddressOf Path.GetFileNameWithoutExtension))
combo.FormattingEnabled = True
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
|
|
|
|
|
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 the OrderByDescending 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
|
|
|
|
|
Hello !
On Entity Framnework 6 , when update model from database , I choose on refresh tab , only one or some of the tables , but after the process finish the entire model is recreated.
Is there any way to force recreating only some of the classes ?
|
|
|
|
|
|
In a Visual basic.net 2010 desktop application, I am loading a list of Access 2013 files that the user needs to select to work with. The code listed below is correct but the file names are too long for the combo box.
Here is the code:
Try
Dim dirAccessFiles As String() = Directory.GetFiles("H:\FilesTest", "*.accdb")
Dim dir As String
For Each dir In dirAccessFiles
cboAccessFile.Items.Add(dir)
Next
Catch except As Exception
Console.WriteLine("The process failed: {0}", e.ToString())
End Try
Thus I have the following questions to ask you:
1. If there is a way to expand the size of the combo box values while the application is executing, can you show me how to do that?
2. Right now in dropdownlist box, the values look like,
'H:\Testfiles\Diane__Currentyear.accdb'.
I would like the value only of 'Diane__Currentyear' that is listed above to display. Also where hardcoded value of 'H:\Testfiles' is displayed in the code above, I will get the actual value from the app. config file.
Thus would you show the code on how to display only the value of 'Diane__Currentyear' in the list of combo box values?
|
|
|
|