Search code examples
c#.netvisual-studio-2010designertypeconverter

How to successfully inherit of TypeConverter with Designer feature


I have a class GaugeScale that I made with a few properties. This class contains the necessary information to display a complete Scale around a GDI+ drawn Gauge.

I have a Gauge control which uses a GaugeDrawer to render it's frame.

The GaugeDrawer are Components which can be used at Designer time like regular components

The frame can be rendered by either a CircularGaugeDrawer (makes a fancy car RPM gauge) or by a ThermometerDrawer...

Anyway, both uses GaugeScale which have some complex properties (GaugeScaleInterval[] and float) and I would like to know if there is a way to allow the Designer of Visual Studio to instantiate and display those properties. It would make the creation of this control way less complex to the users.

So I'm wondering how could I teach the Designer how to use the Drawer components. If TypeConverter are the solution. How can I make them useful to the Designer of Visual Studio

EDIT:

This whole stuff targets WinForm environment.


Solution

  • You've not said it, but I guess you are using WinForms (system.windows.forms).

    In this subject, TypeConverters usually convert textual/string representation to/from a concrete object that can be assigned to a property.

    Please see the article http://msdn.microsoft.com/en-us/magazine/cc164145.aspx - aside from showing int and enum properties, there's also a nice introductory tutorial how to write a custom class Hand along with HandConverter and how to expose a property of usch type in the Properties window of the Forms Designer.

    Please note that the TypeConverter may be registered both at property level and ad the class level. The class-level is best for simple structures like custom numbers/colors/etc that will have a globally-universal converter. More complex cases may register different type converters at property level, so that three properties of the same type "Hand" may use different conversion logic.

    That's for the TypeConverters. This is not the only option. The Properties window is more robust than just that. For example, you surely have seen the Color picker. Aside for the TypeConverters, you may also register Editors and, IIRC, even whole designers. See here for starters: http://msdn.microsoft.com/en-us/library/ms171840.aspx

    Side note:
    Although WinForms seems a little 'dead' after the WPF/XAML has exploded, that old framework and its design-time features are sometimes surprising on how much you can achieve. For example - XAML advertised new feature called "attached properties" - it is quite easy to implement them also here. Even the core localization components use that feature :) However, this can be a quite unforgiving environment, and it is easy to make the VisualStudio unstable. Add to that the all-times-crappy .Designer.cs handling, and some parts of your form's code may sometimes accidentially vanish. Please have backups before you play with editors. TypeConverters are much safer!