Search code examples
c#winformspropertiesattributesembedded-resource

Attribute in C# winforms to specify resource selector?


I made a custom control for WinForms in C#. One of it's properties is an audio stream that will be played at some point. Is there a way I can tell VS that this property should be chosen from the resource manifest (specifically audio resources) with a property or similar?

The property, defined.

The commented property is an audio stream.

Control properties.

This is how it shows in the WinForms designer.

Binary data editor.

This is what you get when you click the edit [...] button. I want this to be replaced by an [audio] resource chooser.


Solution

  • What you are looking for is:

    [Editor(typeof(MyAudioEditor), typeof(UITypeEditor)]
    

    This attribute let you specify a specific editor to be used when that property is displayed in a property grid.

    You can then derive and create your new editor from a base type. The base type must be or must derive from System.Drawing.Design.UITypeEditor.

    In most case, when the Editor is invoked, you pop-up a form of your choice, and bind its return value to your property.

    UITypeEditor has 4 virtual methods and one virtual property that let you change all the behavior when someone interact with your property or when your property is painted in the grid.