1 Replies - 1646 Views - Last Post: 06 October 2011 - 09:11 AM Rate Topic: -----

#1 phenneger  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 6
  • Joined: 23-January 11

Select a cell in listview

Posted 06 October 2011 - 07:44 AM

I am attempting to open a pop-up window when a specific cell is clicked in a listview:

Do While dr.Read()
                If dr("fWeekOfYear") = DatePart("ww", MonthCalendar1.Selectionstart) Then
                    If dr("fFacility") = "Zooniversity 6" Then
                        ListView1.Items(0).UseItemStyleForSubItems = False
                        ListView1.Items(0).SubItems(dr("fDate").dayofweek).BackColor = Color.Blue
                    End If
                    If dr("fFacility") = "Zooniversity 7A" Then
                        ListView1.Items(1).UseItemStyleForSubItems = False
                        ListView1.Items(1).SubItems(dr("fDate").dayofweek).BackColor = Color.Blue
                    End If
                    If dr("fFacility") = "Zooniversity 7B" Then
                        If dr("fStartTime") > 0 And dr("fEndTime") < 12 Then
                            ListView1.Items(2).UseItemStyleForSubItems = False
                            ListView1.Items(2).SubItems(dr("fDate").dayofweek).BackColor = Color.Blue
                        End If
                    End If
                    If dr("fFacility") = "Party Place" Then
                        ListView1.Items(3).UseItemStyleForSubItems = False
                        ListView1.Items(3).SubItems(dr("fDate").dayofweek).BackColor = Color.Blue
                    End If



What is a method for getting the index of the item/subitem?

Thank you for any help you can give!

Larry
larry.phenneger@dallaszoo.com

Is This A Good Question/Topic? 0
  • +

Replies To: Select a cell in listview

#2 phenneger  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 6
  • Joined: 23-January 11

Re: Select a cell in listview

Posted 06 October 2011 - 09:11 AM

I found a solution:

Note that the ListView must be set to FullRowSelect True

  'GET THE ROW AND COLUMN
    Private Sub ListView1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseClick
        Dim col As Integer = getClickedColumn(Me.ListView1, e.X)
        Dim row As Integer = Me.ListView1.SelectedIndices(0)
        Dim cellText As String = Me.ListView1.Items.Item(row).SubItems.Item(col).Text

        MsgBox("Row = " & row & ", Col = " & col & _
               Environment.NewLine & _
               "text = " & Chr(34) & cellText & Chr(34))
    End Sub
    Private Function getClickedColumn(ByVal pListView As ListView, ByVal pMouseX As Integer) As Integer
        Dim result As Integer = 0
        'Get column rights
        Dim colRights As New List(Of Integer)
        Dim colWidths As New List(Of Integer)
        For Each col As ColumnHeader In pListView.Columns
            colWidths.Add(col.Width)
            Dim colRight As Integer = 0 ' - pListView.Columns.Item(0).Width 'Subtract this if you were collecting lefts instead of rights
            For i As Integer = 0 To colWidths.Count - 1
                colRight += colWidths(i)
            Next
            colRights.Add(colRight)
        Next
        'Which column does the mouse X fall inside?
        Dim colIndex As Integer = 0
        For Each colRight As Integer In colRights
            If pMouseX <= colRight Then
                result = colIndex
                Exit For
            End If
            colIndex += 1
        Next
        Return result
    End Function



Hope this helps others!
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1