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.

Currently rated 5.0 by 9 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

Design Time Support

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

About the author

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

Calendar

<<  August 2008  >>
MoTuWeThFrSaSu
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

View posts in large calendar

RecentPosts