954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Error Handling when opening files from ListView

I have a program that lists files found from a search, and when the user selects one of the files and clicks open, it will open the file they've selected. I only want them to be able to open one file at a time, and I want an error box to show if they click Open without selecting a file...does anyone know what error I can use or what kind of conditioning I can use to make this happen? This is what I have:

Private Sub OpenBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenBtn.Click
        Try
            For Each file As ListViewItem In FilesList.Items
                Dim filePath As String = file.SubItems(1).Text & "\" & file.Text
                If file.Selected = True Then
                    Process.Start(filePath)
                End If
            Next
        Catch ex1 As System.ComponentModel.Win32Exception
            MessageBox.Show("Unable to identify a program with which to open this file.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
          End Try
    End Sub
sdimantova
Newbie Poster
15 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

Hi, Use ListView1.SelectedItems instead of ListView1.Items. If ListView1.SelectedItems.Count =0 then There is nothing selected otherwise something selected.

If you want the user should select only one then Change MultiSelect Property to False.

selvaganapathy
Posting Pro
547 posts since Feb 2008
Reputation Points: 44
Solved Threads: 100
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You