G
Guest
I'm attempting to create TypeConverters for the PointF, SizeF, and RectangleF drawing structures. Everything seems to work as expected except when I use a PropertyGrid control to expose properties that utilize the converters. When I do this the text for the properites in the PropertyGrid is always bold, even when the text equals the property's default value. Can somebody please tell me how to support default values when using custom TypeConverts. Here is some sample code:
Public Class Form1
Inherits System.Windows.Forms.Form
#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
Call Me.Initialize()
End Sub
'Form 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()
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 15)
Me.ClientSize = New System.Drawing.Size(292, 258)
Me.Name = "Form1"
Me.Text = "Form1"
End Sub
#End Region
Protected _PropertyGrid As New Windows.Forms.PropertyGrid
Protected _TestClass As New Class1.TestClass
Private Sub Initialize()
Me._PropertyGrid.Dock = DockStyle.Fill
Me._PropertyGrid.SelectedObject = Me._TestClass
Me.Controls.Add(Me._PropertyGrid)
End Sub
End Class
Public Class Class1
#Region " TestClass "
Public Class TestClass
Protected _MyPointF As Drawing.PointF
Protected _MyPoint As Drawing.Point
Public Event MyPointFChanged As System.EventHandler
Public Event MyPointChanged As System.EventHandler
Protected Overridable Overloads Sub OnMyPointFChanged(ByVal e As System.EventArgs)
RaiseEvent MyPointFChanged(Me, e)
End Sub
Protected Overloads Sub OnMyPointFChanged()
Call Me.OnMyPointFChanged(System.EventArgs.Empty)
End Sub
Protected Overridable Overloads Sub OnMyPointChanged(ByVal e As System.EventArgs)
RaiseEvent MyPointChanged(Me, e)
End Sub
Protected Overloads Sub OnMyPointChanged()
Call Me.OnMyPointChanged(System.EventArgs.Empty)
End Sub
<System.ComponentModel.RefreshProperties(System.ComponentModel.RefreshProperties.All), _
System.ComponentModel.Bindable(True), _
System.ComponentModel.DefaultValue(GetType(Drawing.PointF), "0, 0"), _
System.ComponentModel.Browsable(True), _
System.ComponentModel.TypeConverter(GetType(PointFConverter))> _
Public Property MyPointF() As Drawing.PointF
Get
Return Me._MyPointF
End Get
Set(ByVal value As Drawing.PointF)
If (Drawing.PointF.op_Equality(Me._MyPointF, value)) Then Exit Property
Me._MyPointF = value
Call Me.OnMyPointFChanged()
End Set
End Property
<System.ComponentModel.RefreshProperties(System.ComponentModel.RefreshProperties.All), _
System.ComponentModel.Bindable(True), _
System.ComponentModel.DefaultValue(GetType(Drawing.Point), "0, 0"), _
System.ComponentModel.Browsable(True)> _
Public Property MyPoint() As Drawing.Point
Get
Return Me._MyPoint
End Get
Set(ByVal value As Drawing.Point)
If (Drawing.Point.op_Equality(Me._MyPoint, value)) Then Exit Property
Me._MyPoint = value
Call Me.OnMyPointChanged()
End Set
End Property
Public Sub New()
Call MyBase.New()
End Sub
End Class
#End Region
#Region " PointFConverter "
Public Class PointFConverter
Inherits System.ComponentModel.ExpandableObjectConverter
Public Const gcFormatString As String = ""
Public Overloads Overrides Function CanConvertTo( _
ByVal context As System.ComponentModel.ITypeDescriptorContext, _
ByVal destinationType As System.Type) As Boolean
'Return True if destinationType is a class that is supported by this Converter.
If (destinationType Is GetType(Drawing.PointF)) OrElse _
(destinationType Is GetType(System.ComponentModel.Design.Serialization.InstanceDescriptor)) Then
Return True
End If
'If we get here then we must use a method from MyBase since we encountered a type that is not supported by this class.
Return MyBase.CanConvertFrom(context, destinationType)
End Function
Public Overloads Overrides Function CanConvertFrom( _
ByVal context As System.ComponentModel.ITypeDescriptorContext, _
ByVal sourceType As System.Type) As Boolean
'Return True if sourceType is a string.
If (sourceType Is GetType(String)) Then Return True
'If we get here then we must use a method from MyBase since we encountered a type that is not supported by this class.
Return MyBase.CanConvertFrom(context, sourceType)
End Function
Public Overloads Overrides Function ConvertTo( _
ByVal context As System.ComponentModel.ITypeDescriptorContext, _
ByVal culture As System.Globalization.CultureInfo, _
ByVal value As Object, _
ByVal destinationType As System.Type) As Object
If ((TypeOf value Is Drawing.PointF) AndAlso (destinationType Is GetType(System.String))) Then
'Convert value to the appropriate type.
Dim point As Drawing.PointF = DirectCast(value, Drawing.PointF)
'Convert value to a string.
With point
Dim propertyValues() As String = {.X.ToString(gcFormatString), .Y.ToString(gcFormatString)}
Return JoinConverterDestinationStringArray(propertyValues)
End With 'From point
ElseIf ((TypeOf value Is Drawing.PointF) AndAlso (destinationType Is GetType(System.ComponentModel.Design.Serialization.InstanceDescriptor))) Then
MessageBox.Show("Not Supported!")
End If
'If we get here then we must use a method from MyBase since we encountered a type that is not supported by this method.
Return MyBase.ConvertTo(context, culture, value, destinationType)
End Function
Public Overloads Overrides Function ConvertFrom( _
ByVal context As System.ComponentModel.ITypeDescriptorContext, _
ByVal culture As System.Globalization.CultureInfo, _
ByVal value As Object) As Object
If (TypeOf value Is String) Then
Try
'Split value into its individual values.
Dim propertyValues() As String = SplitConverterSourceString(CStr(value))
'Convert the values.
Dim x As Single = CSng(propertyValues(0))
Dim y As Single = CSng(propertyValues(1))
'Return the object that corresponds to value.
Return New Drawing.PointF(x, y)
Catch e As Exception
MessageBox.Show(e.ToString)
End Try
End If
'If we get here then we must use a method from MyBase since we encountered a type that is not supported by this method.
Return MyBase.ConvertFrom(context, culture, value)
End Function
Public Shared Function SplitConverterSourceString(ByVal sourceString As String) As String()
Dim i As Integer
Dim splitString() As String 'Return value.
'Validate sourceString
If (sourceString = "") Then Return Nothing
'Split sourceString into its individual PropertyValues
splitString = Microsoft.VisualBasic.Split(sourceString, ",")
'Make sure that splitString has at least one value
If (splitString Is Nothing) Then Return Nothing
'Trim each of the members in splitString
For i = splitString.GetLowerBound(0) To splitString.GetUpperBound(0)
splitString(i) = Microsoft.VisualBasic.Trim(splitString(i))
Next i
Return splitString
End Function
Public Shared Function JoinConverterDestinationStringArray(ByVal destinationString() As String) As String
'Validate destinationString
If (destinationString Is Nothing) Then Return ""
Return Microsoft.VisualBasic.Join(destinationString, ", ")
End Function
End Class
#End Region
End Class
Thanks for any help!
Lance
Public Class Form1
Inherits System.Windows.Forms.Form
#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
Call Me.Initialize()
End Sub
'Form 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()
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 15)
Me.ClientSize = New System.Drawing.Size(292, 258)
Me.Name = "Form1"
Me.Text = "Form1"
End Sub
#End Region
Protected _PropertyGrid As New Windows.Forms.PropertyGrid
Protected _TestClass As New Class1.TestClass
Private Sub Initialize()
Me._PropertyGrid.Dock = DockStyle.Fill
Me._PropertyGrid.SelectedObject = Me._TestClass
Me.Controls.Add(Me._PropertyGrid)
End Sub
End Class
Public Class Class1
#Region " TestClass "
Public Class TestClass
Protected _MyPointF As Drawing.PointF
Protected _MyPoint As Drawing.Point
Public Event MyPointFChanged As System.EventHandler
Public Event MyPointChanged As System.EventHandler
Protected Overridable Overloads Sub OnMyPointFChanged(ByVal e As System.EventArgs)
RaiseEvent MyPointFChanged(Me, e)
End Sub
Protected Overloads Sub OnMyPointFChanged()
Call Me.OnMyPointFChanged(System.EventArgs.Empty)
End Sub
Protected Overridable Overloads Sub OnMyPointChanged(ByVal e As System.EventArgs)
RaiseEvent MyPointChanged(Me, e)
End Sub
Protected Overloads Sub OnMyPointChanged()
Call Me.OnMyPointChanged(System.EventArgs.Empty)
End Sub
<System.ComponentModel.RefreshProperties(System.ComponentModel.RefreshProperties.All), _
System.ComponentModel.Bindable(True), _
System.ComponentModel.DefaultValue(GetType(Drawing.PointF), "0, 0"), _
System.ComponentModel.Browsable(True), _
System.ComponentModel.TypeConverter(GetType(PointFConverter))> _
Public Property MyPointF() As Drawing.PointF
Get
Return Me._MyPointF
End Get
Set(ByVal value As Drawing.PointF)
If (Drawing.PointF.op_Equality(Me._MyPointF, value)) Then Exit Property
Me._MyPointF = value
Call Me.OnMyPointFChanged()
End Set
End Property
<System.ComponentModel.RefreshProperties(System.ComponentModel.RefreshProperties.All), _
System.ComponentModel.Bindable(True), _
System.ComponentModel.DefaultValue(GetType(Drawing.Point), "0, 0"), _
System.ComponentModel.Browsable(True)> _
Public Property MyPoint() As Drawing.Point
Get
Return Me._MyPoint
End Get
Set(ByVal value As Drawing.Point)
If (Drawing.Point.op_Equality(Me._MyPoint, value)) Then Exit Property
Me._MyPoint = value
Call Me.OnMyPointChanged()
End Set
End Property
Public Sub New()
Call MyBase.New()
End Sub
End Class
#End Region
#Region " PointFConverter "
Public Class PointFConverter
Inherits System.ComponentModel.ExpandableObjectConverter
Public Const gcFormatString As String = ""
Public Overloads Overrides Function CanConvertTo( _
ByVal context As System.ComponentModel.ITypeDescriptorContext, _
ByVal destinationType As System.Type) As Boolean
'Return True if destinationType is a class that is supported by this Converter.
If (destinationType Is GetType(Drawing.PointF)) OrElse _
(destinationType Is GetType(System.ComponentModel.Design.Serialization.InstanceDescriptor)) Then
Return True
End If
'If we get here then we must use a method from MyBase since we encountered a type that is not supported by this class.
Return MyBase.CanConvertFrom(context, destinationType)
End Function
Public Overloads Overrides Function CanConvertFrom( _
ByVal context As System.ComponentModel.ITypeDescriptorContext, _
ByVal sourceType As System.Type) As Boolean
'Return True if sourceType is a string.
If (sourceType Is GetType(String)) Then Return True
'If we get here then we must use a method from MyBase since we encountered a type that is not supported by this class.
Return MyBase.CanConvertFrom(context, sourceType)
End Function
Public Overloads Overrides Function ConvertTo( _
ByVal context As System.ComponentModel.ITypeDescriptorContext, _
ByVal culture As System.Globalization.CultureInfo, _
ByVal value As Object, _
ByVal destinationType As System.Type) As Object
If ((TypeOf value Is Drawing.PointF) AndAlso (destinationType Is GetType(System.String))) Then
'Convert value to the appropriate type.
Dim point As Drawing.PointF = DirectCast(value, Drawing.PointF)
'Convert value to a string.
With point
Dim propertyValues() As String = {.X.ToString(gcFormatString), .Y.ToString(gcFormatString)}
Return JoinConverterDestinationStringArray(propertyValues)
End With 'From point
ElseIf ((TypeOf value Is Drawing.PointF) AndAlso (destinationType Is GetType(System.ComponentModel.Design.Serialization.InstanceDescriptor))) Then
MessageBox.Show("Not Supported!")
End If
'If we get here then we must use a method from MyBase since we encountered a type that is not supported by this method.
Return MyBase.ConvertTo(context, culture, value, destinationType)
End Function
Public Overloads Overrides Function ConvertFrom( _
ByVal context As System.ComponentModel.ITypeDescriptorContext, _
ByVal culture As System.Globalization.CultureInfo, _
ByVal value As Object) As Object
If (TypeOf value Is String) Then
Try
'Split value into its individual values.
Dim propertyValues() As String = SplitConverterSourceString(CStr(value))
'Convert the values.
Dim x As Single = CSng(propertyValues(0))
Dim y As Single = CSng(propertyValues(1))
'Return the object that corresponds to value.
Return New Drawing.PointF(x, y)
Catch e As Exception
MessageBox.Show(e.ToString)
End Try
End If
'If we get here then we must use a method from MyBase since we encountered a type that is not supported by this method.
Return MyBase.ConvertFrom(context, culture, value)
End Function
Public Shared Function SplitConverterSourceString(ByVal sourceString As String) As String()
Dim i As Integer
Dim splitString() As String 'Return value.
'Validate sourceString
If (sourceString = "") Then Return Nothing
'Split sourceString into its individual PropertyValues
splitString = Microsoft.VisualBasic.Split(sourceString, ",")
'Make sure that splitString has at least one value
If (splitString Is Nothing) Then Return Nothing
'Trim each of the members in splitString
For i = splitString.GetLowerBound(0) To splitString.GetUpperBound(0)
splitString(i) = Microsoft.VisualBasic.Trim(splitString(i))
Next i
Return splitString
End Function
Public Shared Function JoinConverterDestinationStringArray(ByVal destinationString() As String) As String
'Validate destinationString
If (destinationString Is Nothing) Then Return ""
Return Microsoft.VisualBasic.Join(destinationString, ", ")
End Function
End Class
#End Region
End Class
Thanks for any help!
Lance