Enigma04 Enigma04 - 1 month ago 5
Vb.net Question

How to load a file from a combobox

I'm currently have the following code:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim dir = "Path\path\path"
For Each file As String In System.IO.Directory.GetFiles(dir)
ComboBox1.Items.Add(System.IO.Path.GetFileNameWithoutExtension(file))
Next

End Sub


This code finds a directory and populates a combobox with names of all the files in that directory. I would like to be able to select the file name from the combobox and the file be opened.

Answer

Double-click on a combobox, when combobox item changes - launch the file listed in the combobox:

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    Dim proc As New System.Diagnostics.Process()
    proc.StartInfo.FileName = ComboBox1.Text
    proc.StartInfo.WorkingDirectory = "Path\path\path"
    proc.Start()
End Sub
Comments