1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
Option Explicit On
Option Strict On
Imports System
Imports System.ComponentModel
Imports System.Globalization
Imports System.Drawing
Imports System.ComponentModel.Design.Serialization
Imports System.Reflection
Public Class PointConverter
Inherits ExpandableObjectConverter
' Overrides the CanConvertFrom method of TypeConverter.
' The ITypeDescriptorContext interface provides the context for the
' conversion. Typically, this interface is used at design time to
' provide information about the design-time container.
Public Overloads Overrides Function CanConvertFrom(context As ITypeDescriptorContext, sourceType As Type) As Boolean
If sourceType Is GetType(String) Then
Return True
End If
Return MyBase.CanConvertFrom(context, sourceType)
End Function
Public Overrides Function CanConvertTo(context As System.ComponentModel.ITypeDescriptorContext, destinationType As System.Type) As Boolean
If destinationType Is GetType(InstanceDescriptor) Then
Return True
End If
Return MyBase.CanConvertTo(context, destinationType)
End Function
' Overrides the ConvertFrom method of TypeConverter.
Public Overloads Overrides Function ConvertFrom(context As ITypeDescriptorContext, culture As CultureInfo, value As Object) As Object
' If converting from a string
If TypeOf value Is String Then
Dim strValue As String = CType(value, String)
If strValue Is Nothing Then Return Nothing
strValue = strValue.Trim()
Dim v As String() =
CStr(value).Split(New String() {"; "}, StringSplitOptions.RemoveEmptyEntries)
Dim intConverter As TypeConverter = TypeDescriptor.GetConverter(GetType(Integer))
Dim value1 As Integer = CType(intConverter.ConvertFromString(context, culture, v(0)), Integer)
Dim value2 As Integer = CType(intConverter.ConvertFromString(context, culture, v(1)), Integer)
Return New Point(value1, value2)
End If
Return MyBase.ConvertFrom(context, culture, value)
End Function
' Overrides the ConvertTo method of TypeConverter.
Public Overloads Overrides Function ConvertTo(context As ITypeDescriptorContext, culture As CultureInfo, value As Object, destinationType As Type) As Object
If destinationType Is Nothing Then Throw New ArgumentNullException("destinationType")
If TypeOf value Is Point Then
If destinationType Is GetType(String) Then
Return CType(value, Point).X & "; " & CType(value, Point).Y
Else ' Convert to InstanceDescriptor
If destinationType Is GetType(InstanceDescriptor) Then
Dim thePoint As Point = CType(value, Point)
Dim properties(1) As Object
Dim types(1) As Type
' propX
types(0) = GetType(Integer)
properties(0) = thePoint.X
' propB
types(1) = GetType(Integer)
properties(1) = thePoint.Y
' Build constructor
Dim ctor As ConstructorInfo = GetType(Point).GetConstructor(types)
Return New InstanceDescriptor(ctor, properties)
End If
End If
End If
Return MyBase.ConvertTo(context, culture, value, destinationType)
End Function
'sans ces 2 methodes on ne voit pas les valeurs devant ThePoint dans le concepteur
' quand X & Y changent
' Role en catimini : elles mettent à jour l' instance Point en fonction des nouvelles valeurs
Public Overloads Overrides Function GetCreateInstanceSupported(
context As ITypeDescriptorContext) As Boolean
' Always force a new instance
Return True
End Function
Public Overloads Overrides Function CreateInstance(
context As ITypeDescriptorContext,
propertyValues As IDictionary) As Object
' Use the dictionary to create a new instance
Return New Point(CType(propertyValues("X"), Integer), CType(propertyValues("Y"), Integer))
End Function
End Class |
Partager