DoubleClick a ListView Control Selected Item to Open a File

Posted on 2004-11-08
Last Modified: 2008-02-01

I have a listview control that lists some files.  I want to be able to doubleclick on the files and have them open.

The code I am trying to use is:

    Private Sub listviewLocalFile_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles listviewLocalFile.DoubleClick

        RunProgram.StartInfo.FileName = "listviewLocalFile.SelectedItems(0).Text" 'SET THE FILE TO OPEN
        RunProgram.StartInfo.Verb = "OPEN" 'SET WHAT TO DO TO THE FILE
        RunProgram.Start() 'RUN THE FILE

    End Sub

When that code runs, I get an, "An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in system.dll.  Additional information: The system cannot find the file specified" error.  An example of what listviewLocalFile.SelectedItems(0).Text contains would be C:\test.txt.

Ben Cunningham
Question by:bcunning
    LVL 2

    Expert Comment

    If you run through with a debugger, can you see what the value of "listviewLocalFile.SelectedItems(0).Text" is?

    Chances are it's just the file name, and not the full file path.

    You may need to prepend the path information to the string...

    PathWeAreCurrentlyViewing = "C:\Path\To\Selected\File\"  ' Note trailing backslash...

    RunProgram.StartInfo.FileName = PathWeAreCurrentlyViewing & "listviewLocalFile.SelectedItems(0).Text"
    LVL 85

    Accepted Solution

       Private Sub ListView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
            Dim lvi As ListViewItem = ListView1.SelectedItems(0)
            If Not (lvi Is Nothing) Then
                    Dim RunProgram As New Process
                    RunProgram.StartInfo.FileName = lvi.Text
                    RunProgram.StartInfo.Verb = "OPEN"
                Catch ex As Exception
                    MessageBox.Show(lvi.Text & vbCrLf & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End Try
            End If
        End Sub

    Author Comment

    Thanks Idle Mind!

    The problem was the quotes in my line:

    RunProgram.StartInfo.FileName = "listviewLocalFile.SelectedItems(0).Text"

    Once I removed the quotes, it worked great!

    Ben Cunningham
    LVL 2

    Expert Comment

    *laughs*... I even rewrote that line and didn't notice the quotes... Silly me. :)

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now