Custom Controls Design Time Support Part 4: TypeConverters Introduction

by Amr Elsehemy 12. January 2008 11:57

 I mentioned in my Introduction post of this series the TypeConverters

Overview: For those who don't know what is a TypeConverter you can expect that it is something responsible to convert between types, there are times when you need to convert from one data type to another. Type converters are classes that describe how a particular object converts to and from other data types. For example, to display a date on the screen(console, windows form, web ..) it will need to be converted to a string representation. Vice Versa is true, if there is a string value of a date that needs to be stored for example in database. Usually casting is enough when the types are simple. But with complex types, a better technique is used.

TypeConverters are classes that define how an object converts to and from other types. Which are used during design time for sting conversion ( used by the PropertyGrid ), also in runtime in validations and conversions.

So, first lets take a look on some of the Common .NET Type Converters that are already built for us to use.

All the following classes are in the System.ComponentModel and the System.Drawing namespaces.

  1. StringConverter
  2. BooleanConverter
  3. CharConverter
  4. CollectionConverter
  5. CultureInfoConverter
  6. DateTimeConverter
  7. EnumConverter
  8. ExpandableObjectConverter
  9. GuidConverter
  10. TimeSpanConverter
  11. ColorConverter
  12. FontConverter
  13. PointConverter
  14. RectangleConverter
  15. SizeConverter

And much more.

From the most Converters that I personally like is the ExpandableObjectConverter you can apply it using the following syntax.

Example:

public class Info
{
int i;
string s;
public int IntProperty
{
get { return i;}
set { i = value; }
}
public int StringProperty
{
get { return s; }
set { s = value; }
}
}

public class ButtonEx : Button
{
...
private Info i;
[TypeConverter(typeof(ExpandableObjectConverter))]
public Info ComplexObject
{
get { return i; }
set { i = value; }
}
}

Without the attribute, the property would look like this
After applying the converter it is like this

Next part I will implement a custom converter.

Tags: , ,

Design Time Support

Comments

1/19/2008 9:29:08 PM #

Joseph Ghassan

Nice tutorials Amr, Design time support for custom controls is a very interesting topic to create customizable controls.

Keep them coming!

Joseph Ghassan Lebanon |

1/19/2008 9:30:43 PM #

Amr

Thanks Joseph, I hope it comes useful tutorial one day.
Regards.

Amr Egypt |

4/19/2008 9:26:20 AM #

GeezerButler

Hi,
I got a sample working using this link http://support.microsoft.com/kb/319626.
I then modified it to bring it as close to your example as possible. I found that you would need to initialize the 'i' varaible (private Info i = new Info();) for your sample to work.

GeezerButler India |

4/19/2008 9:33:40 AM #

Amr

@Greezer
yes thats very true, it seems that I understood your first comment in another way(comparing Converters to Editors)
Any way glad it worked with you now.
Cheers.

Amr Egypt |

4/19/2008 10:52:51 AM #

GeezerButler

The example shown on this page does not work.
Correct me if I am wrong but ExpandableObjectConverter has nothing to do with the property window. I think we would need to write a custom UiTypeEditor for that.

GeezerButler India |

4/19/2008 1:04:23 PM #

Amr

Hello Geezer,
The sample works fine,
The TypeConverter in general work side to side with editors not instead of,
We use any TypeConverter when we need to represent a complex object in a string format and vice versa,
for example the Size property is written as 10,20 , and its Converter named System.Drawing.SizeConverter is responsible for converting that string to a size and vice versa.

About the ExpandableObjectConverter it is a generic converter which converts from a complex object(having multiple Properties) to a TreeLike as shown in the second figure.

Regards.

Amr Egypt |

7/2/2008 12:44:26 AM #

Rob

Hi Amr,

Just wanted to say I have found this, and other articles very helpful in getting started with creating custom ASP.NET controls.

I was having problems getting my own code working, as well as a copy and paste of your code. Also, just to be picky, the StringProperty return type is set to int when it should of course be string.

GeezerButler is indeed correct, in order for the property to display correctly in the designer, it must be initialised via new().

I have spent a LONG time trying to get my code to work and your articles have proved invaluable.

Thank you very much for sharing.

Rob

Rob United Kingdom |

7/8/2008 6:22:54 PM #

Malcolm Hall

Yes thanks GeezerButler your tip solved my problem! In order for the property to display correctly in the designer, it must be initialised via new(). Thus you can't use VS 2008 simple get; set; properties.

Malcolm Hall United Kingdom |

Comments are closed

About the author

Amr Elsehemy
MCSD C#.Net,
MCTS Sql 2005,
MCPD Enterprise
avatar
E-mail me Send mail

Calendar

<<  March 2010  >>
MoTuWeThFrSaSu
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar