Search code examples
vb.netcomboboxdirectorydropdownlistfor

How to add a list of files into a combo box?


I'm currently working on an account system for a game using VB.Net. I was wondering how to make it so that a combo box displays a list of files within a specific directory. Here's what I mean:

When the user runs the application, I want them to see a combo box displaying a directory on their computer.

I've looked at all of the tutorials, but found NOTHING that worked.

NOTE: The combobox is in a dropdownlist style.


Solution

  • VB.NET

    Dim dir = "Your Path"
    For Each file As String In System.IO.Directory.GetFiles(dir)
    ComboBox1.Items.Add(System.IO.Path.GetFileNameWithoutExtension(file))
    Next
    

    C#

    dynamic dir = "Your Path";
    foreach (string file in System.IO.Directory.GetFiles(dir))
    {
     this.comboBox1.Items.Add(System.IO.Path.GetFileNameWithoutExtension(file));
    }
    

    You can visit this post if you want more information about a similar question here