|
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?
|
|
|
|
|
classy_dog wrote: 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? I suggest an alternative, which is more elegant IMO. This article: Adjust combo box drop down list width to longest string width[^] and maybe additionally dock the ComboBox horizontally in its container so that the user can adjust its width by expanding the window horizontally (see the Dock-property of the ComboBox-control in the designer).
classy_dog wrote: I would like the value only of 'Diane__Currentyear' that is listed above to display. Path.GetFileNameWithoutExtension[^] does the trick.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
... and if you don't want that the path is displayed in the Combobox you can write :
cboAccessFile.Items.Add(dir.replace("H:\FilesTest\",""))
|
|
|
|
|
Hello !
I have a partial class that is generated automatically . Each property is defined without Set get.
I have created manually another partial class where I have defined the same properties with Set , get.
But I'm getting error for duplicate properties.
Is anything I can do on my partial class ( that I have created manually ) , in order to not get error messages ? ( I mean without deleting the auto-generated partial classes )
Thank you !
|
|
|
|
|
satc wrote: Each property is defined without Set get. I assume you mean that the auto-generated properties are auto-properties (as properties must have a getter or setter at least, otherwise it would be a field).
One option would be to delete the auto-generated properties from the auto-generated partial class. Which obviously has the disadvantage that you need to do this every time the code is newly generated.
If the code for the getter/setter you're implementing is only for INotifyPropertyChanged and/or similar generic stuff, you could probably edit the T4-template for the code-generator, as previously suggested by Dave.
Or edit the T4-template so that the properties aren't generated at all.
Another potential option would be to name the properties in your manual partial class differently or edit the T4-template to name the auto-generated properties differently (with a prefix or suffix). But I don't know how that would work out with EF; you would have to test it.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|