Designer: Collection of abstract objects

J

Joel G.

Hiya folks,

Does anybody know how to contain a list of abstract objects that is editable
by the designer? For example, say I have the following:


public abstract class Shapes: CollectionBase
{
public Shapes()
{
}

public Shape this[ int index ]
{
get { return (Shape)base.List[ index ]; }
}

public void Add( Shape item )
{
base.List.Add( item );
}

...
...

public class Shape
{
private int area;
public Shape()
{
area=0;
}
public int Area
{
get
{
return area;
}
set
{
area=value;
}
}
}

public class Square : Shape
{
private int size=0;

public Square()
{
}
public int Size
{
get
{
return size;
}
set
{
size=value;
}
}
}

And in my control I have:
[Category("Appearance"),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
PersistenceMode(PersistenceMode.InnerProperty),
EditorAttribute(typeof(ListItemsCollectionEditor), typeof(System.Drawing.Design.UITypeEditor))]
public Shapes Shapes
{
get
{
return shapes;
}
set
{
shapes=value;
}
}


How do I let the user add circles or squares and yet still store all objects
in the same collection?

Joel
 
J

Joel G.

For others benefit, you need to make a CollectionEditor:

using System;
using System.Collections;
using System.Web.UI;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing.Design;
using System.Reflection;

namespace eba.Web
{
/// <summary>
/// Summary description for TestCollection.
/// </summary>
[
Editor(typeof(eba.Web.ShapesCollectionEditor),
typeof(UITypeEditor))
]
public class Shapes: CollectionBase
{
public Shapes()
{
}

public Shape this[ int index ]
{
get { return (Shape)base.List[ index ]; }
}

public void Add( Shape item )
{
base.List.Add( item );
}

/// <summary>
/// Searches for the specified ListColumnDefinition and returns the zero-based
/// index of the first occurrence within the entire CollectionBase.
/// </summary>
/// <param name="item">The ListColumnDefinition to locate in the CollectionBase.
/// </param>
public int IndexOf( Shape item )
{
return base.List.IndexOf( item );
}


/// <summary>
/// Renders all the ListColumnDefinitions which are stored inside the Collection
/// </summary>
/// <param name="output"></param>
/// <param name="inDesignMode"></param>
internal void Render(HtmlTextWriter output, bool inDesignMode)
{
if( true == inDesignMode )
{
// In design mode.
output.Write( "ListColumnItems in design mode" );
}
else
{

}
}
}

public abstract class Shape
{
private int area;
public Shape()
{
area=0;
}
public int Area
{
get
{
return area;
}
set
{
area=value;
}
}
}

public class Square : Shape
{
private int size=0;

public Square()
{
}
public int Size
{
get
{
return size;
}
set
{
size=value;
}
}
}

public class Circle : Shape
{
private int radius=0;

public Circle()
{
}
public int Radius
{
get
{
return radius;
}
set
{
radius=value;
}
}
}


public class ShapesCollectionEditor : CollectionEditor
{
public ShapesCollectionEditor(Type type) : base(type)
{
}
protected override Type[] CreateNewItemTypes()
{
Type[] types = new Type[]
{
typeof (Circle),
typeof(Square)
};
return types;

}
}
}

Hiya folks,

Does anybody know how to contain a list of abstract objects that is
editable by the designer? For example, say I have the following:

public abstract class Shapes: CollectionBase
{
public Shapes()
{
}
public Shape this[ int index ]
{
get { return (Shape)base.List[ index ]; }
}
public void Add( Shape item )
{
base.List.Add( item );
}
...
...
public class Shape
{
private int area;
public Shape()
{
area=0;
}
public int Area
{
get
{
return area;
}
set
{
area=value;
}
}
}
public class Square : Shape
{
private int size=0;
public Square()
{
}
public int Size
{
get
{
return size;
}
set
{
size=value;
}
}
}
And in my control I have:
[Category("Appearance"),

DesignerSerializationVisibility(DesignerSerializationVisibility.Conten
t),
PersistenceMode(PersistenceMode.InnerProperty),
EditorAttribute(typeof(ListItemsCollectionEditor),
typeof(System.Drawing.Design.UITypeEditor))]
public Shapes Shapes
{
get
{
return shapes;
}
set
{
shapes=value;
}
}
How do I let the user add circles or squares and yet still store all
objects in the same collection?

Joel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top