How can Implement a string Collection Editor in a property Grid


Hi I have done some reserch how to implement the above,however I get lost in more complex examples I have found in codeproject and so on.

What I want to do is when showing my propertyGrid I want to have the ability to have a collection editor of strings.

For example sake lets say I want to have a collection of car Models.

THe problem i have is that i see the collection editor but when I press Add I get
"Constructor on type system.string not found"

AM I missing something
If so could somebody show me with a bit of code

Thanks in advance for any suggestions.

I have done as follows:




'//Collection class
#Region "Imports Directives"
Imports System.Collections.ObjectModel
Imports System
Imports System.Collections.Generic
#End Region


Public Class MyCollectionOfTBase(Of T)
    Inherits Collection(Of T)

'//some stuff here I have omitted.

end class


#Region "Imports Directives"
Imports System.ComponentModel
Imports System.Drawing.Design
Imports System.Windows.Forms
#End Region

Public Class Car

private mCars as mycollectionofT(of string)


    <CategoryAttribute("Application"), _
    DesignerSerializationVisibility(DesignerSerializationVisibility.Content), _
        Editor(GetType(System.ComponentModel.Design.CollectionEditor), _
                GetType(System.Drawing.Design.UITypeEditor))> _
        Public Property Cars() As MyCollectionOfT(Of String)
        Get
            Return mcars
        End Get
        Set(ByVal value As MyCollectionOfT(Of String))
            mcars= value
        End Set
    End Property

end class

 



then i map the property grid to the car object.



Answer this question

How can Implement a string Collection Editor in a property Grid

  • Ganeshm

    Hi vbjunkie,

    System.Design.dll assembly has a built-in StringCollectionEditor, use it instead of creating your own CollectionEditor for a collection of strings:

    <Editor("System.Windows.Forms.Design.StringCollectionEditor, System.Design", "System.Drawing.Design.UITypeEditor, System.Drawing")> _
    Public Class MyStringCollection
      ' Implementation of your string collection
    End Class

    Regards,

    -chris



  • Richard Berg MSFT

    This is what I did and still I get the error


    PersonConverter class



    Public Class PersonConverter

    Inherits ExpandableObjectConverter

     

    Public Overloads Overrides Function CanConvertFrom(ByVal context As System.ComponentModel.ITypeDescriptorContext, _

    ByVal sourceType As System.Type) As Boolean

    If sourceType.Equals(GetType(String)) Then

    Return False

    Else

    Return MyBase.CanConvertFrom(context, sourceType)

    End If

    End Function

    Public Overloads Overrides Function CanConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, _

    ByVal destinationType As System.Type) As Boolean

    If destinationType.Equals(GetType(String)) Then

    Return True

    Else

    Return MyBase.CanConvertFrom(context, destinationType)

    End If

    End Function

    Public Overloads Overrides Function ConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, _

    ByVal culture As System.Globalization.CultureInfo, _

    ByVal value As Object, _

    ByVal destinationType As System.Type) As Object

    Dim Parts() As String

    Parts = CType(value, String).Split(New Char() {","})

    Dim p As New Person(Parts(0), Parts(1), Parts(3))

    Return p

    End Function

    Public Overrides Function ConvertFrom(ByVal context As System.ComponentModel.ITypeDescriptorContext, _

    ByVal culture As System.Globalization.CultureInfo, _

    ByVal value As Object) As Object

     

    Return MyBase.ConvertFrom(context, culture, value)

    End Function

    Public Overrides Function GetPropertiesSupported(ByVal context As System.ComponentModel.ITypeDescriptorContext) As Boolean

    Return True

    End Function

    Public Overrides Function GetProperties(ByVal context As System.ComponentModel.ITypeDescriptorContext, _

    ByVal value As Object, ByVal attributes() As System.Attribute) As System.ComponentModel.PropertyDescriptorCollection

    Return MyBase.GetProperties(context, value, attributes)

    End Function

    End Class



     






    Imports System

    Imports System.ComponentModel

    Imports System.Collections.ObjectModel

    <EditorAttribute(GetType(System.ComponentModel.Design.CollectionEditor), _

    GetType(System.Drawing.Design.UITypeEditor))> _

    Public Class PersonCollection(Of t)

    Inherits Collection(Of t)

     

    end Class


     




    Imports System.ComponentModel

    Imports System.ComponentModel.Design

     

    <TypeConverter(GetType(PersonConverter)), _

    Serializable()> _

    Public Class Person

    Private mFirstName As String

    Private mLastName As String

    Private mAge As String

    Private mPeople As PersonCollection(Of String)

     

    Public Sub New(ByVal FirstName As String, _

    ByVal LastName As String, _

    ByVal age As Integer)

    mFirstName = FirstName.Trim

    mLastName = LastName.Trim

    mAge = age

    End Sub

    Public Sub New()

    mFirstName = String.Empty

    mLastName = String.Empty

    mAge = 0

    End Sub

    Public Property FirstName() As String

    Get

    Return mFirstName

    End Get

    Set(ByVal value As String)

    mFirstName = value

    End Set

    End Property

     

    Public Property LastName() As String

    Get

    Return mLastName

    End Get

    Set(ByVal value As String)

    mLastName = value

    End Set

    End Property

     

    Public Property Age() As Integer

    Get

    Return mAge

    End Get

    Set(ByVal value As Integer)

    mAge = value

    End Set

    End Property

    Public Property People() As PersonCollection(Of String)

    Get

    Return mPeople

    End Get

    Set(ByVal value As PersonCollection(Of String))

    mPeople = value

    End Set

    End Property

    End Class



     



    in the form added a property grid and


    Private mPerson As New Person

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Me.PropertyGrid1.SelectedObject = mPerson

    End Sub



     



    get the error pressing add

    any ideas

  • Kousay

    Sorry to bother you again
    Just to say that i still get the error.

    I have found a link that explain but i cannot make it work under 2.0 framework


    http://blogs.msdn.com/toub/archive/2004/10/12/241277.aspx

    can you help

    thanks

  • gjc_vp

    Thanks a lot for the links and time!!


  • Claudio Rossi

    Hi VBJunkie,

    Did you ever get a solution to your question I'm trying to do something similar to your origianl post but seem to get stuck at a similar point.

    Any help would be appriciated



  • Dubouku

    Thanks a lot for your time in trying to help me

    vbjunkie

  • Neil Dela Osa

    Hi vbjunkie,

    Sure, I'll take your code offline and test it later on my notebook, to see if it works on 2.0 framework; rightnow the machine I'm using has no 2.0 framework installed. I'll update you later.


    Regards,

    -chris

  • dickP

    Guys have a look at the msdn page for this.

    http://msdn2.microsoft.com/en-us/library/42737xf2(VS.80).aspx

    take a look at the community content. A neat solution is given there for C#.

    Geethan


  • Adak

    Thats great

    However I would like to learn how to make one as i am planning to use the property grid quite a bit and i wanted to learn how to make my own collection editor.

    Would it be too much too ask you to show me how to do it

    thanks alot

  • zonehenge

     vbjunkie wrote:
    Thanks a lot for your time in trying to help me

    vbjunkie


    Hi vbjunkie,

    I tried your codes with my VB.NET2005, and it works fine. Can you give me the details of your problem regarding the codes you posted or can you email me the zip containing the project at gwapo@models.com


    Thanks,

    -chris

  • mindshoe

    Hi

    Thanks for your reply.
    I have emailed you a zip file containing a small project.
    Press on people and then add in the collection editor and you get the error

    Thanks again

  • ku19832001

    Hi,

    Here's a codeproject article that details the creation of custom CollectionEditor:
    http://www.codeproject.com/csharp/DzCollectionEditor.asp#CollectionEditor

    To get most of the PropertyGrid control, try this article:
    http://msdn.microsoft.com/library/default.asp url=/library/en-us/dndotnet/html/usingpropgrid.asp


    Regards,

    -chris

  • How can Implement a string Collection Editor in a property Grid