'コントロール
Imports System.ComponentModel
Imports System.ComponentModel.Design.Serialization
Public Class MyUserControl
Inherits System.Windows.Forms.UserControl
Private _Items As MyItemCollection = New MyItemCollection
<DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
Public ReadOnly Property Items() As MyItemCollection
Get
Return _Items
End Get
End Property
'その他諸々省略
End Class
'コレクション
Public Class MyItemCollection
Inherits CollectionBase
Default Public ReadOnly Property Item(ByVal index As Integer) As MyItem
'省略
End Property
'その他諸々省略
End Class
'コレクションメンバ
<TypeConverter(GetType(MyItemConverter))> _
Public Class MyItem
Public Sub New()
End Sub
Public Sub New(ByVal [Text] As String)
Me.Text = [Text]
End Sub
Public Sub New(ByVal [Text] As String, ByVal [Value] As String)
Me.Text = [Text]
Me.Value = [Value]
End Sub
Public Property Text() As String
'省略
End Property
Public Property Value() As String
'省略
End Property
End Class
'コレクションメンバ用型コンバータ
Public Class MyItemConverter
Inherits System.ComponentModel.TypeConverter
Public Overloads Overrides Function CanConvertTo(ByVal context As ITypeDescriptorContext, _
ByVal destinationType As System.Type) As Boolean
If destinationType Is GetType(InstanceDescriptor) Then
Return True
Else
Return MyBase.CanConvertTo(context, destinationType)
End If
End Function
Public Overloads Overrides Function ConvertTo(ByVal context As ITypeDescriptorContext, _
ByVal culture As System.Globalization.CultureInfo, _
ByVal value As Object, _
ByVal destinationType As System.Type) As Object
If (destinationType Is GetType(InstanceDescriptor)) Then
Dim [MyItem] As MyItem = CType(value, MyItem)
Dim Constructor As Reflection.ConstructorInfo = _
[MyItem].GetType().GetConstructor(New Type() {GetType(String), GetType(String)})
Return New InstanceDescriptor(Constructor, New Object() {[MyItem].Text, [MyItem].Value})
Else
Return MyBase.ConvertTo(context, culture, value, destinationType)
End If
End Function
End Class
|