Custom Controls Design Time Support Part 4: TypeConverters Introduction

 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. [more]

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

Example:

[code:c#]

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; }
}
}

[/code]

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.


Posted

in

,

by

Tags:

Comments

8 responses to “Custom Controls Design Time Support Part 4: TypeConverters Introduction”

  1. Joseph Ghassan Avatar
    Joseph Ghassan

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

    Keep them coming!

  2. Amr Avatar

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

  3. GeezerButler Avatar
    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.

  4. Amr Avatar

    @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.

  5. GeezerButler Avatar
    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.

  6. Amr Avatar

    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.

  7. Rob Avatar

    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 [b]int[/b] when it should of course be [b]string[/b].

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

    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

  8. Malcolm Hall Avatar
    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.

Leave a Reply

Your email address will not be published. Required fields are marked *