Results 1 to 3 of 3

Thread: Dynamic TypeConverter

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2003
    Location
    India
    Posts
    318

    Dynamic TypeConverter

    Hi,

    I have a combo box and a property grid in a form. The combo box contains 2 items. If one item is selected, two properties are listed in the property grid. These properties should have a dropdown that contains a list of items. I have two string arrays that contain the values for these properties. Can I use one TypeConverter that inherits StringConverter for these lists to be displayed in two different properties? That is, with one TypeConverter class, can I populate both the lists? I will also have to do the same with the other items in the combo box.

    Thanks

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Dynamic TypeConverter

    can you explain exactly how you expect this to work?
    i'm sure there's a simple solution, but your question is unclear

  3. #3
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Dynamic TypeConverter

    Yes it is possible, as for whether it is the best practice, I would say no. You should be creating a new TypeConverter for each property. However, here is how you can do it. Drag a ComboBox and PropertyGrid onto your Form and use the following code:
    Code:
    Imports System.ComponentModel
    
    Public Class Form1
    
        '//methods
        Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
            MyBase.OnLoad(e)
    
            '//add the two types
            Me.ComboBox1.Items.Add(GetType(Item1))
            Me.ComboBox1.Items.Add(GetType(Item2))
    
            '//display the type names
            Me.ComboBox1.DisplayMember = "Name"
    
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) _
                                                   Handles ComboBox1.SelectedIndexChanged
            Dim item = Me.ComboBox1.SelectedItem
            If item IsNot Nothing Then
                Dim type = DirectCast(item, Type)
                '//bind a new instance of that type to the property grid
                Me.PropertyGrid1.SelectedObject = Activator.CreateInstance(type, False)
            End If
        End Sub
    
    End Class
    
    Public Class Item1
    
        Private _name As String
        <TypeConverter(GetType(DynamicItemConverter))> _
        Public Property Name As String
            Get
                Return Me._name
            End Get
            Set(ByVal value As String)
                Me._name = value
            End Set
        End Property
    
        Private _country As String
        <TypeConverter(GetType(DynamicItemConverter))> _
        Public Property Country As String
            Get
                Return Me._country
            End Get
            Set(ByVal value As String)
                Me._country = value
            End Set
        End Property
    
    End Class
    
    Public Class Item2
    
        Private _address As String
        <TypeConverter(GetType(DynamicItemConverter))> _
        Public Property Address As String
            Get
                Return Me._address
            End Get
            Set(ByVal value As String)
                Me._address = value
            End Set
        End Property
    
    End Class
    
    Public Class DynamicItemConverter
        Inherits TypeConverter
    
        '//methods
        Public Overrides Function GetStandardValues _
                (ByVal context As ITypeDescriptorContext) As StandardValuesCollection
    
            If context IsNot Nothing Then
                If context.Instance IsNot Nothing Then
                    If context.PropertyDescriptor IsNot Nothing Then
                        Select Case True
                            Case TypeOf context.Instance Is Item1
                                Select Case context.PropertyDescriptor.Name
    
                                    '//corresponds to the property name of the type
                                    Case "Name"
                                        Return New StandardValuesCollection({"John Smith", _
                                                                             "Jane Smith"})
                                    Case "Country"
                                        Return New StandardValuesCollection({"Europe", _
                                                                             "Canada", _
                                                                             "USA"})
                                End Select
    
                            Case TypeOf context.Instance Is Item2
                                Select Case context.PropertyDescriptor.Name
                                    Case "Address"
                                        Return New StandardValuesCollection({"123 Some Street", _
                                                                             "456 Some Avenue"})
                                End Select
                        End Select
                    End If
                End If
            End If
    
            '//always call the base implementation
            Return MyBase.GetStandardValues(context)
        End Function
    
        Public Overrides Function GetStandardValuesSupported _
                (ByVal context As ITypeDescriptorContext) As Boolean
    
            '//yes, we can supply values for the property grid
            Return True
        End Function
    
    End Class
    Attached Images Attached Images  

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width