none
Filling a ListBox with Folder Names RRS feed

  • Question

  • As the heading say, how do I goabout Filling a ListBox with Folder Names.

    I have tryed this, pleas help correct me where I gone wrong.

     Sub FillCategoryListBox()
    
            Dim RecipeFolder As String = "M:\My Documents\My Resepte\Hoofgeregte\Drankies"
    
            Dim Folders() As String = Directory.GetDirectories(RecipeFolder)
            Me.CategoryListBox.Items.Clear()
            For Each Folder As String In Folders
                CategoryListBox.Items.Add(Path.GetFileName(Folder))
            Next
    
        End Sub

    Sunday, February 23, 2020 11:51 AM

Answers

  • Hi,
    you can use LinQ:

      Sub FillCategoryListBox()
    
        Dim RecipeFolder As String = "M:\My Documents\My Resepte\Hoofgeregte\Drankies"
    
        CategoryListBox.DataSource = (From f In Directory.GetDirectories(RecipeFolder) Select Path.GetFileName(f)).ToList
    
      End Sub


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks


    Sunday, February 23, 2020 1:45 PM

All replies

  • Use Option Strict with Visual Studio Intellisense.

    See how you are just missing the reference for the statements?

     


    Sunday, February 23, 2020 1:41 PM
  • Hi,
    you can use LinQ:

      Sub FillCategoryListBox()
    
        Dim RecipeFolder As String = "M:\My Documents\My Resepte\Hoofgeregte\Drankies"
    
        CategoryListBox.DataSource = (From f In Directory.GetDirectories(RecipeFolder) Select Path.GetFileName(f)).ToList
    
      End Sub


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks


    Sunday, February 23, 2020 1:45 PM