How to Import Filenames to ComboBox in Visual Basic

Written by jaime avelar
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Import Filenames to ComboBox in Visual Basic
Get file names in a directory using VB. (Chad Baker/Digital Vision/Getty Images)

Searching for file names is a common task in computer programming and in Visual Basic you can easily add filenames in a particular path to a combo box. The "GetFiles" method returns the names of files including their paths in a specified directory. Once you have the path and file names, you can extract the file name by using the "GetFileName" method which returns the file name and extension of a specified path string.

Skill level:
Moderately Easy


  1. 1

    Launch Microsoft Visual Basic Express, click "New Project" on the left pane of your computer screen, and double-click "Windows Forms Application" to create a new project.

  2. 2

    Double-click "ComboBox" from the "Toolbox" pane to create a new combo box control. Double-click "Button" to create a new button.

  3. 3

    Double-click "Button1" to create a click event procedure. Type the following code in the very first line of the module to use the "System.IO" namespace:

    Imports System.IO

  4. 4

    Type the following inside the "Button_Click" procedure to create your variables:

    Dim filenames() As String

        Dim result As String
  5. 5

    Get the path and files names in "C:\" by adding the following line of code:

    fileNames = System.IO.Directory.GetFiles("C:\", ".")

  6. 6

    Create a loop and add all the file names to the combo box control by adding the following code:

    For Each file In filenames

            result = Path.GetFileName(file)
  7. 7

    Press "F5" to run your program and click "Button1" to fill the combo box with file names.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.