Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do I make the control designer create the custom editor using a constructor with parameters rather than the default constructor?

You can do this by creating the editor yourself rather than allowing TypeDescriptor to do it as follows.

First, shadow the property you care about in your designer:

protected override void PreFilterProperties( IDictionaryProperties props ) 
{ 
  PropertyDescriptor basePD = props[ "MyProperty" ]; 
  props[ "MyProperty" ] = new EditorPropertyDescriptor( basePD ); 
} 

Second, create a property descriptor that wraps the original descriptor:

private class EditorPropertyDescriptor : PropertyDescriptor 
{ 
  private PropertyDescriptor basePD; 

  public EditorPropertyDescriptor( PropertyDescriptor base ) 
  { 
    basePD = base; 
  } 

  // now, for each property and method, just delegate to the base... 
  public override TypeConverter Converter 
  { get { return basePD.Converter; } }
 
  public override bool CanResetValue( object comp ) 
  { return basePD.CanResetValue( comp ); } 

Third, create your editor by hand when it's asked for:

  public override object GetEditor( Type editorBaseType ) 
  { 
    if ( editorBaseType == typeof( UITypeEditor ) ) 
      return new MyEditor( param1, param2, param3 ); 
    return basePD.GetEditor( editorBaseType ); 
  } 

}

Shawn Burke, Microsoft



Page view counter