473,624 Members | 2,612 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.Componen tModel.Expandab leObjectConvert er 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 2037
Hi Lance,

I think you may also try to inherits the GetProperties and
GetPropertiesSu pported 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.publi c.dotnet.framew ork.windowsform s.designtime

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

' Methods
Public Sub New()

End Sub
Public Overloads Function CanConvertFrom( ByVal context As
ITypeDescriptor Context, ByVal sourceType As Type) As Boolean
If (sourceType Is GetType(String) ) Then
Return True
End If
Return MyBase.CanConve rtFrom(context, sourceType)

End Function
Public Overloads Function CanConvertTo(By Val context As
ITypeDescriptor Context, ByVal destinationType As Type) As Boolean
If (destinationTyp e Is GetType(Instanc eDescriptor)) Then
Return True
End If
Return MyBase.CanConve rtTo(context, destinationType )
End Function
Public Overloads Function ConvertFrom(ByV al context As
ITypeDescriptor Context, ByVal culture As CultureInfo, ByVal value As
Object) As Object
If Not TypeOf value Is String Then
Return MyBase.ConvertF rom(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.Cur rentCulture
End If
Dim ch1 As Char = culture.TextInf o.ListSeparator .Chars(0)
Dim chArray1 As Char() = New Char() {ch1}
Dim textArray1 As String() = text1.Split(chA rray1)
Dim numArray1 As Single() = New Single(textArra y1.Length - 1) {}
Dim converter1 As TypeConverter =
TypeDescriptor. GetConverter(Ge tType(Integer))
Dim num1 As Single
For num1 = 0 To numArray1.Lengt h - 1
numArray1(num1) = CType(converter 1.ConvertFromSt ring(context,
culture, textArray1(num1 )), Integer)
Next num1
If (numArray1.Leng th = 2) Then
Return New PointF(numArray 1(0), numArray1(1))
End If
Dim objArray1 As Object() = New Object() {text1, "x, y"}
End Function
Public Overloads Function ConvertTo(ByVal context As
ITypeDescriptor Context, ByVal culture As CultureInfo, ByVal value As
Object, ByVal destinationType As Type) As Object
If (destinationTyp e Is Nothing) Then
Throw New ArgumentNullExc eption("destina tionType")
End If
If ((destinationTy pe Is GetType(String) ) AndAlso TypeOf value Is
PointF) Then
Dim point1 As PointF = CType(value, PointF)
If (culture Is Nothing) Then
culture = CultureInfo.Cur rentCulture
End If
Dim text1 As String = (culture.TextIn fo.ListSeparato r & " ")
Dim converter1 As TypeConverter =
TypeDescriptor. GetConverter(Ge tType(Single))
Dim textArray1 As String() = New String(2 - 1) {}
Dim num1 As Single = 0
textArray1(num1 ) = converter1.Conv ertToString(con text, culture,
point1.X)
num1 += 1
textArray1(num1 ) = converter1.Conv ertToString(con text, culture,
point1.Y)
Return String.Join(tex t1, textArray1)
End If
If ((destinationTy pe Is GetType(Instanc eDescriptor)) AndAlso TypeOf
value Is Point) Then
Dim point2 As Point = CType(value, Point)
Dim typeArray1 As Type() = New Type() {GetType(Intege r),
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 InstanceDescrip tor(info1, objArray1)
End If
End If
Return MyBase.ConvertT o(context, culture, value, destinationType )
End Function
Public Overloads Function GetProperties(B yVal context As
ITypeDescriptor Context, ByVal value As Object, ByVal attributes As
Attribute()) As PropertyDescrip torCollection
Dim collection1 As PropertyDescrip torCollection =
TypeDescriptor. GetProperties(G etType(PointF), attributes)
Dim textArray1 As String() = New String() {"X", "Y"}
Return collection1.Sor t(textArray1)
End Function
Public Overloads Function GetPropertiesSu pported(ByVal context As
ITypeDescriptor Context) 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 GetCreateInstan ceSupported. 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:
"Customizin g 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 MyPointFConvert er: ExpandableObjec tConverter
{

public override bool CanConvertTo(IT ypeDescriptorCo ntext context, Type
destinationType )
{
if(destinationT ype==typeof(str ing))
{
return true;
}
return base.CanConvert To (context, destinationType );
}

public override object ConvertTo(IType DescriptorConte xt context,
System.Globaliz ation.CultureIn fo culture, object value, Type
destinationType )
{
if(destinationT ype==typeof(str ing))
{
PointF pf=(PointF)valu e;
return "X:"+pf.X.ToStr ing()+ " Y:"+pf.Y.ToStri ng();
}
return base.ConvertTo (context, culture, value, destinationType );
}

public override bool GetCreateInstan ceSupported(ITy peDescriptorCon text
context)
{
return true;
}

public override object CreateInstance( ITypeDescriptor Context context,
IDictionary propertyValues)
{
if (propertyValues != null)
{
return new PointF((float)p ropertyValues["X"],
(float)property Values["Y"]);
}
else
{
return null;
}

}

}

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

[TypeConverter(t ypeof(MyPointFC onverter))]
public PointF Prop
{
get
{
return pt1;
}
set
{
pt1=value;
}
}

private bool ShouldSerialize Prop()
{
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
7458
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 color;etc.). It reads out the structures all the way down to the ICONIMAGE structures. I have this represented as an array of bytes read from the resource. I am using the unmanaged function CreateIconFromResourceEx to generate an hIcon pointer. I take...
13
2715
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 from double to float. Here are the time of test with different grid SIZE and type: SIZE 128 256 512
3
6041
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 from a database and manipulate the TypeConverters dynamically. The property grid then displays the property with a drop down list. It does work when I open the application for the first time. If, however, I open the form and then open a second...
2
1347
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 that they have selected. The items that they select are generally lines of varying length & thickness & some arrows indicating the position of various forces. The users are very happy with the way they use the program at present, but we would...
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). 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...
6
1465
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 then float is. But things are that there is no constructor for Matrix that accepts float. But then, in the System.Math namespace, values like System.Math.PI and function like Sin() and Cos() all return doubles. I can cast, of course, and I do,...
0
862
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 most of the types there should be source code available, that anybody could copy. Does anybody know where to find typeconverters for ColorBlend, StingFormat so on?
12
13932
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
3454
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 inherit from the ExpandableObjectConverter so that child properties are available to the user. That all works swell. Now I'm trying to create a print engine that will be able to print all properties (Browsable). I thought this was going to be...
2
4943
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) unsigned int lst : 16; //2 bytes unsigned int lsv : 16; //2 bytes unsigned int x1 : 24; //3 bytes (float value represented as int)
0
8246
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8685
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8631
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8341
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8490
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6112
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4184
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1489
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.