My program performs a search on a specific directory and returns a list of files with specific criteria. I want the user to be able to select an item in the list and click a button and have that file open...is this possible? The first column of my list gives the file name and the second column gives the file path (without the name). Below is the code I have tried - when I run it and try selecting a file and clicking a button, nothing happens - no errors or anything, but the file doesn't open...any suggestions?

Private Sub OpenBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenBtn.Click
        For Each file As ListViewItem In FilesList.Items
            Dim filePath As String = file.SubItems(1).Text & "\" & file.Text
            If file.Selected = True Then
                MessageBox.Show("You are about to open " & filePath & ".  Are you sure?", "Open File", MessageBoxButtons.YesNo, MessageBoxIcon.Information)
                If DialogResult.Yes Then
                    IO.File.Open(filePath, FileMode.Open)
                ElseIf DialogResult.No Then
                    MsgBox("you decided not to open")
                End If
            End If
        Next
    End Sub

Thanks!

Recommended Answers

All 7 Replies

this line not working : IO.File.Open(filePath, FileMode.Open) Try to change with Process Function :

If DialogResult.Yes Then
         Process.Start(filePath)
ElseIf DialogResult.No Then
         MsgBox("you decided not to open")
End If

That worked, thanks so much!

You're Welcome :)

I have a similar problem. where i have a treeview to display root folders, a listview to display the content of that folder.. but i want to be able to select a file in listview and click a button to display it in a Textbox.

any chance i can get help with this...

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
For Each File As ListViewItem In ListView1.Items
Dim FilePath As String = File.SubItems(0).Text & "\" & File.Text
If File.Selected = True Then
MessageBox.Show("You Are About To Open " & FilePath & ". Proceed?", "Open File", MessageBoxButtons.YesNo, MessageBoxIcon.Information)
If DialogResult.Yes Then
' TextBox2.Text = ListView1.SelectedItems(FilePath, FileMode.Open)
'Process.Start(FilePath, FileMode.Append)
'IO.File.Open(FilePath, FileMode.Open)

' Dim SR As StreamReader(ListView1.SelectedItems)

' Do Until SR.EndOfStream
' TextBox2.AppendText.(SR.ReadLine)
' Loop
' SR.Close()

' 

ElseIf DialogResult.No Then
MessageBox.Show("Operation Cancelled")

End If


End If
Next
End Sub

I have an idea of how to achive it but still not sure on how to geth the answer.

Unsure where to get the path from.
Did check treeview but not very good at VB to accomplish.. any code snippet will be helpful.

The Textfile i am trying to open jus has TEESSSSSST written inside. trying to get that displayed in textBox2 in aid of a button.

any help would be appreciated

Add Items: 1. Listview
2. Button

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'ListView1.CheckBoxes = True
        ListView1.View = View.Tile
    End Sub

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If opd.ShowDialog = Windows.Forms.DialogResult.OK Then
            ListView1.Items.Add(opd.FileName)
        End If
    End Sub

    Private Sub ListView1_ItemSelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles ListView1.ItemSelectionChanged
        For Each File As ListViewItem In ListView1.Items
            Dim FilePath As String = File.SubItems(0).Text ' & "\" & File.Text
            If File.Selected = True Then
                Process.Start(FilePath)
            End If
        Next
    End Sub

firstly sorry because bumping an old thread. i have a similar problem but its in different way of it. i'll try to open a file(exe, lnk, etc etc..) in my listview1 by using the double click function but i keep get the error says "The system cannot find the file specified" everytime i'll double click on the listview
im using this code

Private Sub ListView1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDoubleClick
        For Each file As ListViewItem In ListView1.Items
            Dim filePath As String = file.SubItems(0).Text & "\" & file.Text
            If file.Selected = True Then
                Process.Start(filePath)
            End If
        Next
    End Sub

once again..sorry but i'll need to fix this for my cybercafe project.
any answer would be appreciated
thanks :)

PraDevil

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.