hi all, i've been struggling with this for a few hours now and just can't get it to work... i'm trying to add and remove items in a custom collection using the collectioneditor via a property grid.
i originally started out by inheriting from Generic.List(of ....) which worked, but as i want the collection to inherit from a another class i needed to implement the IList(of ....) and came up with the following:
'code start
Imports System.Drawing.Design
Imports System.Windows.Forms.Design
Public Class Person
Private m_FirstName As String
Public Property FirstName() As String
Get
Return Me.m_FirstName
End Get
Set(ByVal value As String)
Me.m_FirstName = value
End Set
End Property
Private m_LastName As String
Public Property LastName() As String
Get
Return Me.m_LastName
End Get
Set(ByVal value As String)
Me.m_LastName = value
End Set
End Property
Public ReadOnly Property FullName() As String
Get
Return Me.FirstName & " " & Me.LastName
End Get
End Property
End Class
Public Class PersonCollection
'Inherits Generic.List(Of Person)
Implements IList(Of Person)
Protected innerList As List(Of Person) = New List(Of Person)
Public Sub Add(ByVal item As Person) Implements System.Collections.Generic.ICollection(Of Person).Add
CType(innerList, ICollection(Of Person)).Add(item)
End Sub
Public Sub Clear() Implements System.Collections.Generic.ICollection(Of Person).Clear
CType(innerList, ICollection(Of Person)).Clear()
End Sub
Public Function Contains(ByVal item As Person) As Boolean Implements System.Collections.Generic.ICollection(Of Person).Contains
Return CType(innerList, ICollection(Of Person)).Contains(item)
End Function
Public Sub CopyTo(ByVal array() As Person, ByVal arrayIndex As Integer) Implements System.Collections.Generic.ICollection(Of Person).CopyTo
CType(innerList, ICollection(Of Person)).CopyTo(array, arrayIndex)
End Sub
Public ReadOnly Property Count() As Integer Implements System.Collections.Generic.ICollection(Of Person).Count
Get
Return CType(innerList, ICollection(Of Person)).Count
End Get
End Property
Public ReadOnly Property IsReadOnly() As Boolean Implements System.Collections.Generic.ICollection(Of Person).IsReadOnly
Get
Return CType(innerList, ICollection(Of Person)).IsReadOnly
End Get
End Property
Public Function Remove(ByVal item As Person) As Boolean Implements System.Collections.Generic.ICollection(Of Person).Remove
Return CType(innerList, ICollection(Of Person)).Remove(item)
End Function
Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of Person) Implements System.Collections.Generic.IEnumerable(Of Person).GetEnumerator
Return CType(innerList, IEnumerable(Of Person)).GetEnumerator()
End Function
Public Function IndexOf(ByVal item As Person) As Integer Implements System.Collections.Generic.IList(Of Person).IndexOf
Return CType(innerList, IList(Of Person)).IndexOf(item)
End Function
Public Sub Insert(ByVal index As Integer, ByVal item As Person) Implements System.Collections.Generic.IList(Of Person).Insert
CType(innerList, IList(Of Person)).Insert(index, item)
End Sub
Default Public Property Item(ByVal index As Integer) As Person Implements System.Collections.Generic.IList(Of Person).Item
Get
Return CType(innerList, IList(Of Person)).Item(index)
End Get
Set(ByVal value As Person)
CType(innerList, IList(Of Person)).Item(index) = value
End Set
End Property
Public Sub RemoveAt(ByVal index As Integer) Implements System.Collections.Generic.IList(Of Person).RemoveAt
CType(innerList, IList(Of Person)).RemoveAt(index)
End Sub
Public Function GetEnumerator1() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
Return CType(innerList, IEnumerable).GetEnumerator()
End Function
Public Sub AddRange(ByVal items As Person())
For Each tmpItem As Person In items
Me.Add(tmpItem)
Next
End Sub
End Class
Public Class Office
Private m_People As PersonCollection
<System.ComponentModel.Editor(GetType(System.ComponentModel.Design.CollectionEditor), GetType(UITypeEditor))> _
Public Property People() As PersonCollection
Get
If Me.m_People Is Nothing Then
Me.m_People = New PersonCollection
End If
Return Me.m_People
End Get
Set(ByVal value As PersonCollection)
Me.m_People = value
End Set
End Property
End Class
'code end
after adding this class to a property grid and clicking on the (...) button to edit the collection, the collection editor pops up but the add / remove buttons are disabled. I've read on the codeproject website that there are 3 items that a collection must have in order for them to be persisted by the collection editor, namely implement IList, item property and an add / addrange sub... which i've done (i think)
can anyone shed some light on this, any help would be great
Cheers
Mark
[EDIT: code indented]