Lisa
Lisa

Reputation: 3

Disable listview item in vb.net

I am trying to disable item (row) in my list view but its seem there no option like .enable = false and I tried to find anything to get my item to by disable but visible. Is there anything like that? If the user is allowed to select it then the item is enabled else it's visible but not enabled.

I have a table in the database that admin will fill it in which the user can view the window or not, so I want the user to able to see it and if not allowed to view it then its disable.

Upvotes: 0

Views: 2201

Answers (1)

Mary
Mary

Reputation: 15101

This only works if MultiSelect is set to False and the .Tag property is set for every item. (Yes or No).

Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
        If Not ListView1.SelectedItems.Count = 0 Then
            Dim item As ListViewItem = ListView1.SelectedItems(0)
            If item.Tag.ToString = "No" Then
                item.Selected = False
            End If
        End If
End Sub

As per @ jmcilhinney , The following code should work with MultiSelect = True. I tried to access the last item added to the collection but it seems that the SelectedItems collection is ordered the same as the order the items appear in the ListView; not as expected the last item added would be last in the collection..

Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
        If Not ListView1.SelectedItems.Count = 0 Then
            For Each item As ListViewItem In ListView1.SelectedItems
                If item.Tag.ToString = "No" Then
                    item.Selected = False
                End If
            Next
        End If
End Sub

Upvotes: 1

Related Questions