473,499 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

TypeConverters for Float Drawing Structures

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

Nov 21 '05 #1
6 2014
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.des igntime

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.

Nov 21 '05 #2
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

Nov 21 '05 #3
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.

Nov 21 '05 #4
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/de...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.

Nov 21 '05 #5
Thanks! That is exactly what I was looking for. I am very grateful.

Lance

Nov 21 '05 #6
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.

Nov 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
7444
by: WALDO | last post by:
I have an interesting quandary. I have developed a program in VB.Net to extract an icon resource from an exe/dll or from an ico file and enumerate its formats (16x16,256 color;32x32,true...
13
2692
by: Michele Guidolin | last post by:
Hello to everybody. I'm doing some benchmark about a red black Gauss Seidel algorithm with 2 dimensional grid of different size and type, I have some strange result when I change the computation...
3
6035
by: Dave Girvitz | last post by:
I have a PropertyGrid (Windows Forms App) based component that uses TypeConverters to generate ranges of acceptable values for properties. The idea was that I could download the key/value pairs...
2
1334
by: Terry Holland | last post by:
We have an application that is written in VB that allows to users to design some basic structures by allowing them to select items in in a Picture box and them modify the properties of the item...
0
306
by: ljlevend | last post by:
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)....
6
1461
by: Martijn Mulder | last post by:
System.Drawing.Drawing2D.Matrix comes in two flavors, int and float. When doing graphics, you will you use float. Type double would be better, since it is, when well applied, simply more acurate...
0
855
by: herpers | last post by:
Hi, I'm writing a control with a lot of properties, that are not directly supported by the propertygrid. I can work around this by writing typeconverters, that do the job for me, but I think for...
12
13859
by: vandanasridhar | last post by:
Hi myself vandana. I wanna know how can we use float variables with in C structures. Can anyone help me.
3
3423
by: =?Utf-8?B?R3JlZyBDbGFyaw==?= | last post by:
I have an object which I present to the user through a propertyGrid. With many of the properties (many of which are objects themselves) I have implemented my own TypeConverters; many of these...
2
4917
by: (2b|!2b)==? | last post by:
I have a struct declared as follows: struct RecordType1 { unsigned int dt : 24; //3 bytes unsigned int ts : 16; //2 bytes unsigned int lsp : 24; //3 bytes (float value represented as int)...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7171
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6893
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7386
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4918
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3098
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3090
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
664
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.