1,105,592 Community Members

Open File from Listview?

Member Avatar
sdimantova
Newbie Poster
15 posts since Jun 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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!

Member Avatar
Jx_Man
Senior Poster
3,543 posts since Nov 2007
Reputation Points: 987 [?]
Q&As Helped to Solve: 542 [?]
Skill Endorsements: 74 [?]
Featured
 
0
 

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
Question Answered as of 5 Years Ago by Jx_Man
Member Avatar
sdimantova
Newbie Poster
15 posts since Jun 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

That worked, thanks so much!

Member Avatar
Jx_Man
Senior Poster
3,543 posts since Nov 2007
Reputation Points: 987 [?]
Q&As Helped to Solve: 542 [?]
Skill Endorsements: 74 [?]
Featured
 
0
 

You're Welcome :)

Member Avatar
HarvinderSingh
Newbie Poster
4 posts since Aug 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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...

Member Avatar
HarvinderSingh
Newbie Poster
4 posts since Aug 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
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

Member Avatar
shadow.arnab
Newbie Poster
1 post since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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
Member Avatar
PraDevil
Newbie Poster
1 post since Feb 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article