双击vb.net打开listview项目

时间:2012-01-10 16:07:14

标签: vb.net listview onclick 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

这是代码的重要部分,我想到使用onclick但我似乎无法随心所欲地提出任何建议?

我还考虑使用和打开文件对话框,但我不认为没有用户输入路径就可以完成

1 个答案:

答案 0 :(得分:4)

我假设当你说打开时,你的意思是你想在该文件类型的默认程序中打开相关文件。在这种情况下,您需要在列表视图中存储文件的完整路径。这可以通过以下代码完成:

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

        li.Tag = file.FullName
    Next

然后,您需要为listview的双击方法添加事件。在该事件中,您将要查看所选项目并为其运行默认程序。

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