Debugging custom UITypeEditors

If you read this you’re probably gonna think: What a moron! Anyway, i’m sharing this in the hope that i’ll be the last to undergo the following. In order to test my custom UITypeEditor i did the following:

  1. Create a UserControl
  2. Add a property to the control
  3. Add an Editor attribute to the property
  4. Build
  5. Drag a UserControl on the designer form
  6. Test via Visual Studio’s Property Window if the UITypeEditor works as expected
  7. Everytime i changed some code: Restart Visual Studio, load the project and repeat 6.

A tedious task to say the least. Yesterday i figured out that i could drag a PropertyGrid on the designer form, and set it’s SelectedObject property to a class with a property that uses the custom UITypeEditor; Instead of having to reload visual studio i can simply start a debug session, and click on the property in the PropertyGrid. Now it’s a breeze to develop custom UITypeEditors :)

private void Form1_Load(object sender, EventArgs e)
{
 // display an instance of PersonEntry,
 // a class with a property that should use the custom UITypeEditor i want to test
 this.propertyGrid1.SelectedObject = new PersonEntry(new Person("Tim", "Van Wassenhove"));
}

public class PersonEntry
{
 ...

 // instruct the PropertyGrid to use my custom PersonUITypeEditor
 [Editor(typeof(PersonUITypeEditor), typeof(UITypeEditor))]
 public Person Person
 {
  get { return this.person; }
  set { this.person = value; }
 }
}

public class PersonUITypeEditor : UITypeEditor
{
 public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
 {
  return UITypeEditorEditStyle.Modal;
 }

 public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
 {
  Person person = value as Person;

  IWindowsFormsEditorService svc = context.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
  if (svc != null)
  {
   using (PersonEditorForm personEditorForm = new PersonEditorForm(person))
   {
    if (svc.ShowDialog(personEditorForm) == DialogResult.OK)
    {
     return personEditorForm.Person;
    }
   }
  }

  return value;
 }
}
  1. Thanks for this article. You saved me some time!

  2. Thanks for sharing this tip!

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>