How to disable fiew items in Listview?

As for TreeView I like to create my own items.
This way I can have a better control of what will happen.

Tho you have 2 options:

1. Item text based recognition
Just add the items you want to the ListView and, on the ListView's SelectedIndexChanged Event, do the following:

Visual Basic:
    Private Sub ListView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
        Select Case Me.ListView1.SelectedItems(0).Text
            Case "AAA"
                 'Do something 
            Case "BBB"
                 'Do something 
        End Select
    End Sub

2. Inner Item handle
This is a more 'PRO' approach, it may suite you on other, more complex, issues (I believe this is not the case).
Just create as many custom ListViewItems as task you want to perform and add them to the ListView.
Each of these custom items will "know" how to perform their task.
See the example bellow:
Visual Basic:
    Public MustInherit Class BaseCustomNode
        Inherits ListViewItem

        'Implement only the contructors you need.
        Public Sub New(ByVal text As String, ByVal imageindex As Integer)
            Me.Text = text
            Me.ImageIndex = imageindex
        End Sub

        Public MustOverride Sub ExecuteTask() 'May have arguments

    End Class
    Public Class CustomItem1
        Inherits BaseCustomNode

        Public Sub New(ByVal text As String, ByVal imageindex As Integer)
            MyBase.New(text, imageindex)
        End Sub

        Public Overrides Sub ExecuteTask()
            'Execute this items code
        End Sub
    End Class
    Public Class CustomItem2
        Inherits BaseCustomNode

        Public Sub New(ByVal text As String, ByVal imageindex As Integer)
            MyBase.New(text, imageindex)
        End Sub

        Public Overrides Sub ExecuteTask()
            'Execute this items code
        End Sub
    End Class

    'Assuming thet the ListView's parent form is Form1...
    Dim CItem1 As New CustomItem1("MyItem1", 0)
    Dim CItem2 As New CustomItem2("MyItem2", 1)
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ListView1.Items.AddRange(New ListViewItem() {CItem1, CItem2})
    End Sub

    Private Sub ListView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
        CType(Me.ListView1.SelectedItems(0), BaseCustomNode).ExecuteTask()
    End Sub


Last Note:

I believe that my 1st option will suit your needs for this current task, but it's allways good to learn something ahead... :D

Alex :P
 
Just one more thing...

If have miss undetood you and you only want to desable the item on the listview, based on the items selection try to hide the items (remove them from the listview) instead of desable them...

ListViewItems Desabling insn't a standard functionality, so you would have to owner draw the item and implement some SelectIndexChanged functionality to the ListView itself...

Alex :p
 
Back
Top