MaskedInput Transform Intellisense

Posted Mon Oct 27, 2008 @ 7:06 AM


MaskedInput class's Transform property that I described in depth in a few preceding blog posts presented us with an interesting design dilemma. At first it seemed like it should be a typical enumeration property, with one value for each Transform type. However, since the architecture allowed new Transform types to be added in a number of different ways (see the previous blog posts), the enum turned out to be too stifling. For that reason, we opted for a string instead.

However, we still wanted to offer some Intellisense, to give the developer a hint at the values he would most likely want to use. We did it by placing the TypeConverterAttribute on the Transform property:
public sealed class MaskedInput : BaseInput
{
  ...
  [TypeConverter(typeof(MaskedInputTransformConverter))]
  public string Transform
  {
    get {...}
    set {...}
  }
  ...
}

The implementation of MaskedInputTransformConverter is very simple:
/// <summary>
/// Provides Visual Studio intellisense for Transform property.
/// The list of StandardValues here corresponds to the properties of client-side ComponentArt_MaskedInput_Transforms object.
/// </summary>
public class MaskedInputTransformConverter : TypeConverter
{
  public string[] StandardValues =
  {
    "empty", 
    "CreditCard_VisaMasterCard",
    "CreditCard_AmEx",
    "Telephone_NorthAmerica",
    "ZipCode",
    "PostalCode",
    "PostalCode_Australia",
    "EmailAddress"
  };
  public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
  {
    return new StandardValuesCollection(StandardValues);
  }
  public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
  {
    return true;
  }
}

This works really well in ASPX Intellisense, popping up the list of standard values, without forcing you to use one of them - exactly the behaviour we needed. The Intellisense also works for the Properties pane. Unfortunately it does not work in the CodeBehind files, and I don’t think there is a way to implement it there.

Comments

Posted on Mon Oct 27, 2008 @ 7:06 AM

Although you won't get direct intellisense, I'd also recommend having a public static class that contains public static readonly strings for each of the types. Something like: public static class StandardTypes { public static readonly string Empty = "empty"; public static readonly string CreditCard_VistaMasterCard = "CreditCard_VisaMasterCard"; ... } That way, people can at least say Transform = StandardTypes.ZipCode; and not have to worry about string literals. Alternatively you could use an enum and transform from the enum to the string representation. I'd be interested in knowing if using an enum type converter would reveal intellisense in the codebehind. Anyway, one last thing to consider. I wonder what would happen if you used a partial method or a delegate/event that allowed third-party transform developers to "hook" into the type converter and add values. GetStandardValues could simply call the partial method, delegate, or event to allow the standard list to be "widened" with the additional values. All of this is just theory. If none of it works, or if I've misspoken, then my apologies :) Cheers -eli

Posted on Mon Oct 27, 2008 @ 7:06 AM

Some very interesting ideas, thank you.  I will look into this as soon as I get a chance.  Probably later this week.  I'll report back if I am able to get any of these techniques to work.

Click here to log in or create an account.