vb.net - 单击列表视图子项并打开窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16719202/
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-17 13:42:51  来源:igfitidea点击:

vb.net - click on listview subitem and open window

vb.netvisual-studio-2010listview

提问by J_D

I'm not sure if this is possible after doing a bunch of googling, but hopefully it is. I have an application that pulls a list of information from a MySQL database and populates a listview (Unfortunately, I can't change to a datagrid at this time.) What I'm tasked to do is make it so that when clicking on a certain column, a window will open and, based on the ID of the row that was clicked on, retrieve another set of results from the same database.

在做了一堆谷歌搜索之后,我不确定这是否可能,但希望是。我有一个应用程序,它从 MySQL 数据库中提取信息列表并填充列表视图(不幸的是,我目前无法更改为数据网格。)我的任务是制作它以便在单击特定列,将打开一个窗口,并根据单击的行的 ID,从同一数据库中检索另一组结果。

The list view is created as such:

列表视图是这样创建的:

   Do While result.Read()
        Dim siteid = (result.Item("idsite").ToString())
        Dim sitename = (result.Item("name").ToString())
        Dim last_import_date = (result.Item("import_finished").ToString())
        Dim last_import_file = (result.Item("file_name").ToString())
        Dim last_line = (result.Item("last_line").ToString())
        Dim status = (result.Item("status").ToString())

        Dim lv As ListViewItem = ListView1.Items.Add(siteid)
        lv.SubItems.Add(sitename)
        lv.SubItems.Add(last_import_date)
        lv.SubItems.Add(last_import_file)
        lv.SubItems.Add(last_line)
        lv.SubItems.Add(status)
    Loop

So preferably I'd like to click on "Last_import_file" and have that open the window. I've tried a bunch of ItemClicked type commands, but haven't had much luck.

所以最好我想点击“Last_import_file”并打开窗口。我尝试了一堆 ItemClicked 类型的命令,但运气不佳。

Is what I'm attempting possible? I don't need any special text formatting, just want to register the click and pop open the dialog.

我正在尝试的可能吗?我不需要任何特殊的文本格式,只想注册点击并弹出对话框。

Thanks!

谢谢!

回答by Alex

Yes it is possible. To do this in a Listview it is a bit more complicated than a DataGridView. You'll need to make use of the ListViewHitTestInfoclass. Using the MouseDownEvent of your listview, use this code:

对的,这是可能的。要在 Listview 中执行此操作,它比 DataGridView 复杂一些。你需要利用这个ListViewHitTestInfo类。使用MouseDown列表视图的事件,使用以下代码:

Private Sub ListView1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDown
    Dim info As ListViewHitTestInfo = ListView1.HitTest(e.X, e.Y)
    MsgBox(info.Location.ToString())
    If Not IsNothing(info.SubItem) Then
        'info will contain the information of the clicked listview column. You can then go through it's subitems for more information, if any.
    End If
End Sub