473,405 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 2008
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
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.