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.
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