ali samah
ali samah

Reputation: 1

opening files added to a combobox

Dim dir = "..//Football/"
Private Sub FTablebutton_Click(sender As Object, e As EventArgs) Handles FTablebutton.Click

    For Each file As String In System.IO.Directory.GetFiles(dir)
        FfilesComboBox.Items.Add(System.IO.Path.GetFileNameWithoutExtension(file))
    Next

End Sub

Private Sub FfilesComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles FfilesComboBox.SelectedIndexChanged
    Dim openfile As String = System.IO.Path.Combine(dir, FfilesComboBox.SelectedItem.ToString)
    'start the process using the openfile string
    Process.Start(openfile)
End Sub

I am able to add all the files to combobox but the problem is i cannot open the file when selected from the combobox

Upvotes: 0

Views: 604

Answers (2)

Hadi
Hadi

Reputation: 37313

If you Then use Visual studio 2008 or newer. you can use Anonymous class to store the full file path with the FileNameWithoutExtension.

    Dim dir = "..//Football/"

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        FfilesComboBox.DisplayMember = "Text"

End Sub

Private Sub FTablebutton_Click(sender As Object, e As EventArgs) Handles FTablebutton.Click

    For Each file As String In System.IO.Directory.GetFiles(Dir)
        FfilesComboBox.Items.Add(New With {.Text = System.IO.Path.GetFileNameWithoutExtension(file), .Value = file})
    Next

End Sub

you can use ComboBox1.SelectedItem.Value to get the value (that is the file full path)

Private Sub FfilesComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles FfilesComboBox.SelectedIndexChanged

    'start the process using the openfile string
    Process.Start(FfilesComboBox.SelectedItem.Value)
End Sub

This will work even you choose to loop over sub directories.

This code is tested and worked fine

Upvotes: 0

r-sniper
r-sniper

Reputation: 1483

Try this

Private Sub FTablebutton_Click(sender As Object, e As EventArgs) Handles FTablebutton.Click

    For Each file As String In System.IO.Directory.GetFiles(dir)
        FfilesComboBox.DisplayMember = "key"
        FfilesComboBox.ValueMember = "value"
        FfilesComboBox.Items.Add(New DictionaryEntry(System.IO.Path.GetFileNameWithoutExtension(file), System.IO.Path.GetFileName(file)))
    Next

End Sub

Private Sub FfilesComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles FfilesComboBox.SelectedIndexChanged
    Dim openfile As String = System.IO.Path.Combine(dir, FfilesComboBox.SelectedItem.Value.ToString)
    'start the process using the openfile string
    Process.Start(openfile)
End Sub

Upvotes: 1

Related Questions