Intercettare l’evento ItemCheck della CheckedListBox

Sfortunatamente la CheckedListBox non possiede un evento che si scatena dopo che è stato “flaggato” un item, percui è difficile poter interagire direttamente con l’elemento selezionato.

E’ possibile aggirare il problema creando degli items personalizzati visto che all’interno della CheckedListBox è possibile aggiungere qualunque oggetto indicizzato (che espone l’interfaccia IDictionary).

Creiamo una classe personalizzata che rappresenti un item e che abbia una proprietà di tipo CheckState che memorizzi il flag impostato dalla CheckedListBox che lo contiene.

' Oggetto personalizzato myChekedItem.
Public Class myChekedItem
    Private _Nome As String
    Property Nome() As String
        Get
            Return _Nome
        End Get
        Set(ByVal value As String)
            _Nome = value
        End Set
    End Property

    Private _Stato As CheckState
    Property Stato() As CheckState
        Get
            Return _Stato
        End Get
        Set(ByVal value As CheckState)
            _Stato = value
        End Set
    End Property

    Public Sub New(ByVal nome As String, ByVal stato As CheckState)
        Me.Nome = nome
        Me.Stato = stato
    End Sub

    Public Overrides Function ToString() As String
        Return Me.Nome
    End Function
End Class

Eseguendo l’overrides della funzione .ToString della classe base, permetteremo alla CheckedListBox di visualizzare automaticamente la proprietà Nome nell’item personalizzato, senza dover impostare la proprietà (nascosta) DisplayMember.

 

Popoliamo la CheckedListBox con gli items personalizzati.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Aggiunge gli oggetti myChekedItem alla CheckedListBox.
        Me.CheckedListBox1.Items.Add(New myChekedItem("prova Item personalizzato 1", CheckState.Indeterminate))
        Me.CheckedListBox1.Items.Add(New myChekedItem("prova Item personalizzato 2", CheckState.Indeterminate))
    End Sub

Intercettiamo l’evento .ItemCheck della CheckedListBox e risaliamo all’item personalizzato selezionato:

    Private Sub CheckedListBox2_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
        ' Imposta il checkstate all'oggetto myChekedItem selezionato.
        Dim chkItem As myChekedItem = DirectCast(Me.CheckedListBox1.Items(e.Index), myChekedItem)
        chkItem.Stato = e.NewValue

        ' Aggiunge l'item selezionato.
        If e.NewValue = CheckState.Checked Then
            Me.ListBox1.Items.Add(chkItem)
        ElseIf e.NewValue = CheckState.Unchecked Then
            Me.ListBox1.Items.Remove(chkItem)
        End If
    End Sub

All’interno dell’evento .ItemCheck gestiremo i nostri items come oggetti indipendenti sfruttandone tutte le loro funzionalità. In questo caso li abbiamo aggiunti o rimossi ad una ListBox sfruttandone i metodi .Items.Add e .Items.Remove a seconda del valore della loro prorietà Stato.

 

Print | posted on lunedì 14 dicembre 2009 17.32

Feedback

No comments posted yet.

Your comment:





 
Please add 7 and 1 and type the answer here:

Copyright © Andrea Zingoni

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski