双击打开列表视图项目 vb.net

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8806402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:52:31  来源:igfitidea点击:

Opening listview items with a double click vb.net

vb.netlistviewonclickdouble-click

提问by user1100407

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?

这是代码的重要部分,我想使用 onclick 但我似乎无法使用它,有什么建议吗?

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

我也考虑过使用和打开文件对话框,但我认为没有用户输入路径就无法完成

回答by briddums

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