TypeConverters for Float Drawing Structures

G

Guest

I need to create TypeConverters for the floating type drawing structures
(i.e., PointF, SizeF and RectangleF) that behave exactly as their integer
couterparts (e.g., System.Drawing.PointConverter). I am able to create
TypeConverters with basic functionality by inheriting from
System.ComponentModel.ExpandableObjectConverter and overriding CanConvertTo,
CanConvertFrom, ConvertTo and ConvertFrom. But, the integer converts also
implement the following advanced behaviors in PropertyGrids that I am unable
to duplicate:

1. Expanded items (i.e., X, Y, Width and Height) are disabled if the
corresponding property is ReadOnly.
2. Expanded items are drawn in the normal (i.e., not bold) state if the
corresponding property is set to its default value.

Does anybody know how to implement these two behaviors?

Thank you,
Lance
 
P

Peter Huang [MSFT]

Hi Lance,

I think you may also try to inherits the GetProperties and
GetPropertiesSupported method.

Here is some code for your reference.
NOTE: the code has not been detailed tested, you may need to change
according to your scenario.

Thanks!
BTW:
For designtime issue, we have a specified newsgroup as below.
microsoft.public.dotnet.framework.windowsforms.designtime

Dim pt As New PointF(1.1F, 2.2F)
<TypeConverter(GetType(PointFConverter))> _
Public ReadOnly Property TestPt() As PointF
Get
Return pt
End Get
End Property
End Class
Public Class PointFConverter
Inherits ExpandableObjectConverter

' Methods
Public Sub New()

End Sub
Public Overloads 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 Function CanConvertTo(ByVal context As
ITypeDescriptorContext, ByVal destinationType As Type) As Boolean
If (destinationType Is GetType(InstanceDescriptor)) Then
Return True
End If
Return MyBase.CanConvertTo(context, destinationType)
End Function
Public Overloads Function ConvertFrom(ByVal context As
ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As
Object) As Object
If Not TypeOf value Is String Then
Return MyBase.ConvertFrom(context, culture, value)
End If
Dim text1 As String = CType(value, String).Trim
If (text1.Length = 0) Then
Return Nothing
End If
If (culture Is Nothing) Then
culture = CultureInfo.CurrentCulture
End If
Dim ch1 As Char = culture.TextInfo.ListSeparator.Chars(0)
Dim chArray1 As Char() = New Char() {ch1}
Dim textArray1 As String() = text1.Split(chArray1)
Dim numArray1 As Single() = New Single(textArray1.Length - 1) {}
Dim converter1 As TypeConverter =
TypeDescriptor.GetConverter(GetType(Integer))
Dim num1 As Single
For num1 = 0 To numArray1.Length - 1
numArray1(num1) = CType(converter1.ConvertFromString(context,
culture, textArray1(num1)), Integer)
Next num1
If (numArray1.Length = 2) Then
Return New PointF(numArray1(0), numArray1(1))
End If
Dim objArray1 As Object() = New Object() {text1, "x, y"}
End Function
Public Overloads Function ConvertTo(ByVal context As
ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As
Object, ByVal destinationType As Type) As Object
If (destinationType Is Nothing) Then
Throw New ArgumentNullException("destinationType")
End If
If ((destinationType Is GetType(String)) AndAlso TypeOf value Is
PointF) Then
Dim point1 As PointF = CType(value, PointF)
If (culture Is Nothing) Then
culture = CultureInfo.CurrentCulture
End If
Dim text1 As String = (culture.TextInfo.ListSeparator & " ")
Dim converter1 As TypeConverter =
TypeDescriptor.GetConverter(GetType(Single))
Dim textArray1 As String() = New String(2 - 1) {}
Dim num1 As Single = 0
textArray1(num1) = converter1.ConvertToString(context, culture,
point1.X)
num1 += 1
textArray1(num1) = converter1.ConvertToString(context, culture,
point1.Y)
Return String.Join(text1, textArray1)
End If
If ((destinationType Is GetType(InstanceDescriptor)) AndAlso TypeOf
value Is Point) Then
Dim point2 As Point = CType(value, Point)
Dim typeArray1 As Type() = New Type() {GetType(Integer),
GetType(Integer)}
Dim info1 As ConstructorInfo =
GetType(Point).GetConstructor(typeArray1)
If (Not info1 Is Nothing) Then
Dim objArray1 As Object() = New Object() {point2.X,
point2.Y}
Return New InstanceDescriptor(info1, objArray1)
End If
End If
Return MyBase.ConvertTo(context, culture, value, destinationType)
End Function
Public Overloads Function GetProperties(ByVal context As
ITypeDescriptorContext, ByVal value As Object, ByVal attributes As
Attribute()) As PropertyDescriptorCollection
Dim collection1 As PropertyDescriptorCollection =
TypeDescriptor.GetProperties(GetType(PointF), attributes)
Dim textArray1 As String() = New String() {"X", "Y"}
Return collection1.Sort(textArray1)
End Function
Public Overloads Function GetPropertiesSupported(ByVal context As
ITypeDescriptorContext) As Boolean
Return True
End Function
End Class


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Peter,

Thank you very much for the sample. That is the idea that I am looking for,
but the sample does not address the two behaviors of
System.Drawing.PointConverter that I am trying to duplicate, namely:

1. Expanded items (i.e., X, Y, Width and Height) are disabled (i.e., grayed)
if the
corresponding property is ReadOnly.
2. Expanded items are drawn in the normal (i.e., not bold) state if the
corresponding property is set to its default value.

Do you know how System.Drawing.PointConverter implements these behaviors?

Thanks again,
Lance
 
J

Jeffrey Tan[MSFT]

Hi Lance,

For this issue, we will do some research into PointConverter class. We will
update you ASAP. Thanks for your understanding.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Lance,

Sorry for letting you wait for so long.

#1, It seems the difference arises from the fact that the PointConverter
returns true for GetCreateInstanceSupported. This causes the grid to gray
out the properties when the Point property is read only.
#2, We should provided a "default value" for this property, then when the
property's value equals the "default value", the property will not become
bold. To set the "default value" for the proproperty, we may provided a
ShouldSerialize[property name]() method for the control. For more
information, please refer to the "Basic Code Generation Concepts" section
in the below article:
"Customizing Code Generation in the .NET Framework Visual Designers"
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/ht
ml/custcodegen.asp

The full code snippet lists below(I have tested it on my side, which works
well):

public class MyPointFConverter: ExpandableObjectConverter
{

public override bool CanConvertTo(ITypeDescriptorContext context, Type
destinationType)
{
if(destinationType==typeof(string))
{
return true;
}
return base.CanConvertTo (context, destinationType);
}

public override object ConvertTo(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value, Type
destinationType)
{
if(destinationType==typeof(string))
{
PointF pf=(PointF)value;
return "X:"+pf.X.ToString()+ " Y:"+pf.Y.ToString();
}
return base.ConvertTo (context, culture, value, destinationType);
}

public override bool GetCreateInstanceSupported(ITypeDescriptorContext
context)
{
return true;
}

public override object CreateInstance(ITypeDescriptorContext context,
IDictionary propertyValues)
{
if (propertyValues != null)
{
return new PointF((float)propertyValues["X"],
(float)propertyValues["Y"]);
}
else
{
return null;
}

}

}

private PointF pt1=new PointF(0, 0);

[TypeConverter(typeof(MyPointFConverter))]
public PointF Prop
{
get
{
return pt1;
}
set
{
pt1=value;
}
}

private bool ShouldSerializeProp()
{
return this.Prop!=new PointF(1, 1);
}
If you still have anything unclear, please feel free to tell me. Thanks
==================================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Lance,

I am glad my reply makes sense to you. If you need further help, please
feel free to tell me, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top