Paul Selormey

Hello All,

Please help me solve this problem. I want to create an editor that will handle the following situation.

I have an abstract class Marker, and many different implements that will not be exposed

to the user.

The markers are created through a factory system, MarkerFactory, and I have a collection of the markers in a Control that I will to create the editor for.

public class Marker

{

protected Marker(...){}

}

internal class AMarker : Marker

{

}

internal class BMarker : Marker

{

}

public class MarkerFactory

{

public Marker Create(...) {return newMarker;}

}

public class MarkerControl : Control

{

[MarkerDesigner]

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content )]

public IList<Marker> Markers

{

get{ return markerList;}

}

}

Any guide/hint as to how I could create such an UITypeEditor, that will work with the

factory system.

Best regards,

Paul.



Re: Windows Forms Designer UITypeEditor and Factory Creation

Malacki

If I understand what you're asking, you would create a UITypeEditor derived from CollectionEditor and override the CreateInstance() method.

Code Snippet

public class MarkerControl : Control

{

private List<Marker> markerList;

public MarkerControl()

{

// ... stuff ...

markerList = new List<Marker>();

}

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

public List<Marker> Markers

{

get { return markerList; }

}

}

public class Marker

{

string someProperty;

protected Marker() { someProperty = this.GetType().Name; }

public string SomeProperty

{

get { return someProperty; }

}

}

internal class AMarker : Marker

{

}

internal class BMarker : Marker

{

}

public class MarkerFactory

{

public static Marker Create(Type markerType)

{

if (typeof(Marker).IsAssignableFrom(markerType))

return TypeDescriptor.CreateInstance(null, markerType, null, null) as Marker;

MessageBox.Show (String.Format("MarkerFactory: {0} is not a Marker type.", markerType.Name));

return null;

}

}

public class MarkerEditor : CollectionEditor

{

public MarkerEditor(Type type) : base(type) { }

protected override object CreateInstance(Type itemType)

{

return MarkerFactory.Create(itemType);

}

}

Hope that helps.

Marc.