J_D
J_D

Reputation: 681

vb.net - click on listview subitem and open window

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.

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.

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!

Upvotes: 2

Views: 11136

Answers (1)

Alex
Alex

Reputation: 4938

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 ListViewHitTestInfo class. Using the MouseDown Event of your listview, use this code:

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

Upvotes: 5

Related Questions