Search code examples
vb.netvisual-studio-2010propertygridcollectioneditor

Custom CollectionEditor never triggers a property's "set" method


I am trying to implement a way of persisting a collection in a custom settings class. I have successfully created the settings class (inheriting ApplicationSettingsBase) and can save properties using the built-in editors on a PropertyGrid, but my custom implementation of a property grid for collections doesn't persist any of the values I add. Here's my code:

Imports System.Configuration
Imports System.ComponentModel
Imports System.Drawing.Design
Imports System.ComponentModel.Design

Public Class CustomSettings
    Inherits ApplicationSettingsBase

    <UserScopedSetting()> _
    <DefaultSettingValue("White")> _
    Public Property BackgroundColor() As Color
        Get
            BackgroundColor = Me("BackgroundColor")
        End Get
        Set(ByVal value As Color)
            Me("BackgroundColor") = value
        End Set
    End Property

    <UserScopedSetting()> _
    <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
    <Editor(GetType(CustomStringCollectionEditor), GetType(UITypeEditor))> _
    Public Property EmailAddresses() As Collection
        Get
            EmailAddresses = Me("EmailAddresses")
        End Get
        Set(ByVal value As Collection)
            Me("EmailAddresses") = value
        End Set
    End Property
End Class

Public Class CustomStringCollectionEditor
    Inherits CollectionEditor

    Public Sub New()
        MyBase.New(GetType(Collection))
    End Sub

    Protected Overrides Function CreateInstance(ByVal itemType As System.Type) As Object
        Return String.Empty
    End Function

    Protected Overrides Function CreateCollectionItemType() As System.Type
        Return GetType(String)
    End Function
End Class

I set a breakpoint on the Set methods for both the BackgroundColor property and the EmailAddresses property. The BackgroundColor property works as it should - it breaks on the Set statement and stores the property correctly. But when I close the custom CollectionEditor dialog, the EmailAddresses "Set" method is never called. How can I get my custom editor to actually save the property once it's done being edited?


Solution

  • I think I fixed it (with help from this question). I added an override to the EditValue function in my custom editor. Here is the code:

        Public Overrides Function EditValue(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal provider As System.IServiceProvider, ByVal value As Object) As Object
            Dim result As Object = MyBase.EditValue(context, provider, value)
            DirectCast(context.Instance, CustomSettings).EmailAddresses = DirectCast(result, List(Of String))
            Return result
        End Function
    

    I also moved from a collection to a list - I read somewhere that was a safer way to go. I also added a constructor to my CustomSettings class that set the EmailAddresses property to a new List(Of String) if it was unset to begin with. I found that the first time it ran, I could edit the list and add items, but they wouldn't be persisted:

    Public Sub New()
        If Me("EmailAddresses") Is Nothing Then
            Me("EmailAddresses") = New List(Of String)
        End If
    End Sub
    

    And now it's all working like it should. But if this isn't the best way or there's an easier way to do it, please chime in.