Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. vb.net combo box

vb.net combo box

Scheduled Pinned Locked Moved Visual Basic
csharptutorialquestion
21 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Wombaticus

    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.

    S Offline
    S Offline
    Sascha Lefevre
    wrote on last edited by
    #10

    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

    W C 2 Replies Last reply
    0
    • S Sascha Lefevre

      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

      W Offline
      W Offline
      Wombaticus
      wrote on last edited by
      #11

      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! :) )

      S C 2 Replies Last reply
      0
      • W Wombaticus

        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! :) )

        S Offline
        S Offline
        Sascha Lefevre
        wrote on last edited by
        #12

        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

        W L 2 Replies Last reply
        0
        • S Sascha Lefevre

          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

          W Offline
          W Offline
          Wombaticus
          wrote on last edited by
          #13

          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>

          S 1 Reply Last reply
          0
          • W Wombaticus

            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>

            S Offline
            S Offline
            Sascha Lefevre
            wrote on last edited by
            #14

            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

            1 Reply Last reply
            0
            • S Sascha Lefevre

              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

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #15

              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.

              1 Reply Last reply
              0
              • S Sascha Lefevre

                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

                Richard DeemingR Offline
                Richard DeemingR Offline
                Richard Deeming
                wrote on last edited by
                #16

                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

                "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                S 1 Reply Last reply
                0
                • Richard DeemingR Richard Deeming

                  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

                  S Offline
                  S Offline
                  Sascha Lefevre
                  wrote on last edited by
                  #17

                  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

                  1 Reply Last reply
                  0
                  • W Wombaticus

                    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! :) )

                    C Offline
                    C Offline
                    CHill60
                    wrote on last edited by
                    #18

                    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.

                    1 Reply Last reply
                    0
                    • S Sascha Lefevre

                      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

                      C Offline
                      C Offline
                      classy_dog
                      wrote on last edited by
                      #19

                      Thanks for your helpful answers!

                      S 1 Reply Last reply
                      0
                      • C classy_dog

                        Thanks for your helpful answers!

                        S Offline
                        S Offline
                        Sascha Lefevre
                        wrote on last edited by
                        #20

                        You're welcome!

                        If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                        1 Reply Last reply
                        0
                        • W Wombaticus

                          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. :)

                          J Offline
                          J Offline
                          Jens Madsen Hojby
                          wrote on last edited by
                          #21

                          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)
                          ' 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

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups