Ok,
Here is the full code.
Total of three files:
Name.vb - The "data" class with two properties.
NameConverter.vb - The TypeConverter class for class Name
NewTextBox.vb - A control inheriting from TextBox with
additional property of type Name.
After you build it, start a new windows project,
register "NewTextBox" in your tool box and add it to a
form.
You can now change the value of MyProperty using the
designer and it will show up on your form constructor
(hence, from String -> Name -> InstanceDescriptor)
however, the simple String Assignmnet doesn't work.
Hope you can help me.
Thanks
'Name.vb
Imports System.ComponentModel
<TypeConverter(GetType(NameConverter))> _
Public Class Name
Public Sub New()
End Sub
Public Sub New(ByVal FirstName As String, ByVal
LastName As String)
Me.New()
Me.Last = LastName
Me.First = FirstName
End Sub
Private _Last As String
Public Property Last() As String
Get
Return _Last
End Get
Set(ByVal Value As String)
_Last = Value
End Set
End Property
Private _First As String
Public Property First() As String
Get
Return _First
End Get
Set(ByVal Value As String)
_First = Value
End Set
End Property
End Class
'NameConverter.vb
Imports System
Imports System.ComponentModel
Imports System.Globalization
Public Class NameConverter
Inherits TypeConverter
Public Overloads Overrides Function CanConvertFrom
(ByVal context As ITypeDescriptorContext, ByVal sourceType
As Type) As Boolean
If sourceType Is GetType(String) Then
Return True
End If
Return MyBase.CanConvertFrom(context, sourceType)
End Function
Public Overloads Overrides Function ConvertFrom(ByVal
context As ITypeDescriptorContext, ByVal culture As
CultureInfo, ByVal value As Object) As Object
If TypeOf value Is String Then
Dim v As String() = CStr(value).Split(New Char
() {" "c})
Return New Name(v(0), v(1))
End If
Return MyBase.ConvertFrom(context, culture, value)
End Function
Public Overloads Overrides Function CanConvertTo(ByVal
context As System.ComponentModel.ITypeDescriptorContext,
ByVal destinationType As System.Type) As Boolean
If destinationType Is GetType
(System.ComponentModel.Design.Serialization.InstanceDescrip
tor) Then
Return True
End If
If destinationType Is GetType(String) Then
Return True
End If
Return MyBase.CanConvertTo(context,
destinationType)
End Function
Public Overloads Overrides Function ConvertTo(ByVal
context As ITypeDescriptorContext, ByVal culture As
CultureInfo, ByVal value As Object, ByVal destinationType
As Type) As Object
If destinationType Is GetType(String) Then
Return CType(value, Name).First & " " & CType
(value, Name).Last
End If
If destinationType Is GetType
(System.ComponentModel.Design.Serialization.InstanceDescrip
tor) Then
'This will generate a call using New Name
(First,Last)
Dim ci As System.Reflection.ConstructorInfo =
GetType(Name).GetConstructor(New Type() {GetType(String),
GetType(String)})
Dim t As Name = CType(value, Name)
Return New
System.ComponentModel.Design.Serialization.InstanceDescript
or(ci, New Object() {t.First, t.Last})
End If
Return MyBase.ConvertTo(context, culture, value,
destinationType)
End Function
End Class
'NewTextBox.vb
Public Class NewTextBox
Inherits System.Windows.Forms.TextBox
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form
Designer.
InitializeComponent()
'Add any initialization after the
InitializeComponent() call
_MyProperty = New Name()
Dim s As String = "Liron Kedem"
'_MyProperty = s
End Sub
'UserControl1 overrides dispose to clean up the
component list.
Protected Overloads Overrides Sub Dispose(ByVal
disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the
Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
'
'NewTextBox
'
Me.Name = "NewTextBox"
End Sub
#End Region
Private _MyProperty As Name
Public Property MyProperty() As Name
Get
Return _MyProperty
End Get
Set(ByVal Value As Name)
_MyProperty = Value
End Set
End Property
End Class