Imports System.ComponentModel
Imports System.Globalization
' Expandable TypeConverter for the DrawStyles class
Public Class GradientStyleConverter
Inherits ExpandableObjectConverter
Private m_lastValue As GradientStyle = New GradientStyle
Public Overloads Overrides Function CanConvertFrom(ByVal context As ITypeDescriptorContext, ByVal t As Type) As Boolean
' Indicate which types this class can convert "From"
If t Is GetType(String) Then
Return True
End If
Return MyBase.CanConvertFrom(context, t)
End Function
Public Overloads Overrides Function ConvertFrom(ByVal context As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object) As Object
If value.GetType() Is GetType(String) Then
Try
' Attempt to parse the string representation
' into the constituent properties
Dim gs As New GradientStyle
Dim s As String = CType(value, String)
Dim c As String
Dim i As Integer
Dim props As String() = s.Split(",".ToCharArray())
gs.GradientMode = CType([Enum].Parse(GetType(LinearGradientMode), Trim(props(0))), LinearGradientMode)
c = Trim(props(1))
If Not IsNumeric(c) Then
gs.PanelColor1 = Color.FromKnownColor(CType([Enum].Parse(GetType(KnownColor), c), KnownColor))
i = 2
Else
gs.PanelColor1 = Color.FromArgb(Convert.ToInt32(c), Convert.ToInt32(Trim(props(2))), Convert.ToInt32(Trim(props(3))))
i = 4
End If
c = Trim(props(i))
If Not IsNumeric(c) Then
gs.PanelColor2 = Color.FromKnownColor(CType([Enum].Parse(GetType(KnownColor), c), KnownColor))
Else
gs.PanelColor2 = Color.FromArgb(Convert.ToInt32(c), Convert.ToInt32(Trim(props(i + 1))), Convert.ToInt32(Trim(props(i + 2))))
End If
Return gs
Catch ex As Exception
' Revert back to the last good value if an error is encountered
' trying to parse the entered "text" value for the constiuent properties
Return m_lastValue
End Try
End If
MyBase.ConvertFrom(context, culture, value)
End Function
Public Overloads Overrides Function CanConvertTo(ByVal context As ITypeDescriptorContext, ByVal t As Type) As Boolean
' Indicate which types this class can convert "To"
If t Is GetType(GradientStyle) Then Return True
Return MyBase.CanConvertTo(context, t)
End Function
Public Overloads Overrides Function ConvertTo(ByVal context As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object, ByVal destType As Type) As Object
If destType Is GetType(String) Then
' Attempt to convert the value into a string representation
Try
Dim styles As GradientStyle = CType(value, GradientStyle)
m_lastValue = styles
Return String.Format("{0}, {1}, {2}", styles.GradientMode, ColorToString(styles.PanelColor1), ColorToString(styles.PanelColor2))
Catch ex As Exception
' If there was an error, display it instead of the string value
Return String.Format("Error: {0}", ex.Message)
End Try
End If
Return MyBase.ConvertTo(context, culture, value, destType)
End Function
Public Overloads Overrides Function GetPropertiesSupported(ByVal context As ITypeDescriptorContext) As Boolean
' Indicate the we support returning a list
' of the properties this type contains
Return True
End Function
Public Overloads Overrides Function GetProperties(ByVal context As ITypeDescriptorContext, ByVal value As Object, ByVal attributes As Attribute()) As PropertyDescriptorCollection
' Return a collection of the Properties that make up this type
Dim collection As PropertyDescriptorCollection = TypeDescriptor.GetProperties(GetType(GradientStyle), attributes)
Dim props As String() = New String() {"GradientMode", "PanelColor1", "PanelColor2"}
Return collection.Sort(props)
End Function
Public Overloads Overrides Function GetCreateInstanceSupported(ByVal context As System.ComponentModel.ITypeDescriptorContext) As Boolean
' Indicate that creating an instance is supported
Return True
End Function
Public Overloads Overrides Function CreateInstance(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal propertyValues As System.Collections.IDictionary) As Object
' Create a new instance of the Gradient Style
' with the supplied property values
Dim obj1 As Object = propertyValues("GradientMode")
Dim obj2 As Object = propertyValues("PanelColor1")
Dim obj3 As Object = propertyValues("PanelColor2")
If obj1 Is Nothing Then obj1 = LinearGradientMode.Mode1
If obj2 Is Nothing Then obj1 = Color.Red
If obj3 Is Nothing Then obj1 = Color.Blue
Dim gs As New GradientStyle
gs.GradientMode = obj1
gs.PanelColor1 = obj2
gs.PanelColor2 = obj3
Return gs
End Function
Private Function ColorToString(ByVal theColor As Color) As String
' Converts a Color into an appropriately formatted string
' For Known Colors the name is returned, otherwise the RGB value is returned
' in the format "R, G, B"
If Not [Enum].IsDefined(GetType(KnownColor), theColor.Name) Then
Return String.Format("{0}, {1}, {2}", theColor.R, theColor.G, theColor.B)
Else
Return theColor.Name
End If
End Function
End Class