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 3 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

    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