J_Snape
J_Snape

Reputation: 31

Opening listview items with a double click vb.net

I want to open items from a list view with a double click.

Imports System.IO
Imports System.Xml
Public Class cv7import

Private Sub cv7import_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim caminho As String
    caminho = "C:\Documents and Settings\Software\Ambiente de trabalho\cv7import"



    lstvicon.View = View.Details
    lstvicon.GridLines = False
    lstvicon.FullRowSelect = True
    lstvicon.HideSelection = False
    lstvicon.MultiSelect = True


    lstvicon.Columns.Add("Nome")
    lstvicon.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)


    Dim DI As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(caminho)

    Dim files() As System.IO.FileInfo = DI.GetFiles

    Dim file As System.IO.FileInfo

    Dim li As ListViewItem
    For Each file In files
        li = lstvicon.Items.Add(file.Name)


    Next

End Sub



Private Sub btnimp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnimp.Click

    Dim caminho As String
    caminho = "C:\Documents and Settings\Software\Ambiente de trabalho\cv7import"


    Dim items() As ListViewItem = lstvicon.SelectedItems.Cast(Of ListViewItem).ToArray
    Dim csv() As String = Array.ConvertAll(items, Function(lvi) String.Join(",", lvi.SubItems.Cast(Of ListViewItem.ListViewSubItem).Select(Function(si) si.Text).ToArray))
    IO.File.WriteAllLines("C:\Documents and Settings\Software\Ambiente de trabalho\cv7import\teste.csv", csv)

End Class

That's the important part of the code, I was think of using onclick but I cant seem to get anywhere with it, any suggestions?

I also considered using and Open File Dialog but I dont think it can be done without the user's input of a path

Upvotes: 2

Views: 22951

Answers (1)

briddums
briddums

Reputation: 1846

I'm assuming that when you say open, you mean you want to open the associated file in the default program for that file type. In that case, you need to be storing the full path to the file in the list view. That can be accomplished via this code:

    For Each file In files
        li = lstvicon.Items.Add(file.Name)

        li.Tag = file.FullName
    Next

You will then need to add an event for the listview's double-click method. Within that event you'll want to look at the selected item and run the default program for it.

Private Sub lstvicon_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstvicon.DoubleClick
    Process.Start(lstvicon.SelectedItems(0).Tag)
End Sub

Upvotes: 4

Related Questions