locked
Listview Rows and Columns RRS feed

  • Question

  • I need to be able to identify the column and row index for a listview. I want to be able to click the column and automatically give the the location of the row and column clicked.

    Thank you.,

    Sunday, May 12, 2013 8:50 PM

Answers

  • Hi,

     Here is a way to get the Row Index, Column Index, and the Text of the row and column of the clicked item in a ListView. The FullRowSelect must be set True for this to work. You can use the ListView_DoubleClick event or the ListView_Click event like this

    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'This property can be set in the designer window instead of here
            ListView1.FullRowSelect = True
        End Sub
    
        Private Sub ListView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.Click
            Dim sp As New Point(MousePosition.X - Me.PointToScreen(ListView1.Location).X, MousePosition.Y - Me.PointToScreen(ListView1.Location).Y)
            Dim ht As ListViewHitTestInfo = ListView1.HitTest(sp)
            Select Case ht.Location
                Case ListViewHitTestLocations.Label
                    Dim cwb, cwe, cnt As Integer
                    For Each col As ColumnHeader In ListView1.Columns
                        cwe += col.Width
                        If sp.X > cwb And sp.X < cwe Then
                            Exit For
                        End If
                        cnt += 1
                        cwb = cwe
                    Next
                    TextBox1.Text = "Selected Text=" & ht.SubItem.Text & "   Row Index=" & ht.Item.Index.ToString & "   Column Index=" & cnt.ToString
                Case Else
                    TextBox1.Text = "Selected Text=" & ht.Item.Text & "   Row Index=" & ht.Item.Index.ToString & "   Column Index=0"
            End Select
        End Sub
    End Class


    • Edited by IronRazerz Sunday, May 12, 2013 11:26 PM
    • Proposed as answer by Adnan Dedic Monday, May 13, 2013 12:02 AM
    • Marked as answer by Youen Zen Monday, June 10, 2013 9:18 AM
    Sunday, May 12, 2013 11:25 PM
  • Hey,

     I don`t think there is a scrollBar value you can get but, you can get the (TopItem.Index) which tells you which row index is showing at the top of the Listview.

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim indx As Integer = ListView1.TopItem.Index
            MessageBox.Show(indx.ToString)
        End Sub


    Please don`t forget to mark my above post as the answer so others can find it when looking for an answer for the same question.  :)

    • Edited by IronRazerz Monday, May 13, 2013 2:20 AM
    • Marked as answer by Youen Zen Monday, June 10, 2013 9:18 AM
    Monday, May 13, 2013 2:01 AM

All replies

  • Hi,

     Here is a way to get the Row Index, Column Index, and the Text of the row and column of the clicked item in a ListView. The FullRowSelect must be set True for this to work. You can use the ListView_DoubleClick event or the ListView_Click event like this

    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'This property can be set in the designer window instead of here
            ListView1.FullRowSelect = True
        End Sub
    
        Private Sub ListView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.Click
            Dim sp As New Point(MousePosition.X - Me.PointToScreen(ListView1.Location).X, MousePosition.Y - Me.PointToScreen(ListView1.Location).Y)
            Dim ht As ListViewHitTestInfo = ListView1.HitTest(sp)
            Select Case ht.Location
                Case ListViewHitTestLocations.Label
                    Dim cwb, cwe, cnt As Integer
                    For Each col As ColumnHeader In ListView1.Columns
                        cwe += col.Width
                        If sp.X > cwb And sp.X < cwe Then
                            Exit For
                        End If
                        cnt += 1
                        cwb = cwe
                    Next
                    TextBox1.Text = "Selected Text=" & ht.SubItem.Text & "   Row Index=" & ht.Item.Index.ToString & "   Column Index=" & cnt.ToString
                Case Else
                    TextBox1.Text = "Selected Text=" & ht.Item.Text & "   Row Index=" & ht.Item.Index.ToString & "   Column Index=0"
            End Select
        End Sub
    End Class


    • Edited by IronRazerz Sunday, May 12, 2013 11:26 PM
    • Proposed as answer by Adnan Dedic Monday, May 13, 2013 12:02 AM
    • Marked as answer by Youen Zen Monday, June 10, 2013 9:18 AM
    Sunday, May 12, 2013 11:25 PM
  • Worked perfectly! Just what I was looking for!

    Let me ask you another question, regarding listview events, how do you get the value of the scrollbar when using listview procedures?

    Thank you in advance.

    Monday, May 13, 2013 1:09 AM
  • Hey,

     I don`t think there is a scrollBar value you can get but, you can get the (TopItem.Index) which tells you which row index is showing at the top of the Listview.

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim indx As Integer = ListView1.TopItem.Index
            MessageBox.Show(indx.ToString)
        End Sub


    Please don`t forget to mark my above post as the answer so others can find it when looking for an answer for the same question.  :)

    • Edited by IronRazerz Monday, May 13, 2013 2:20 AM
    • Marked as answer by Youen Zen Monday, June 10, 2013 9:18 AM
    Monday, May 13, 2013 2:01 AM