Designer: Collection of abstract objects

Discussion in 'ASP .Net Building Controls' started by Joel G., Mar 3, 2006.

  1. Joel G.

    Joel G. Guest

    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
     
    Joel G., Mar 3, 2006
    #1
    1. Advertising

  2. Joel G.

    Joel G. Guest

    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
    >
     
    Joel G., Mar 3, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DaKoadMunky
    Replies:
    4
    Views:
    552
    Lee Weiner
    Apr 20, 2004
  2. Matthias Kaeppler
    Replies:
    1
    Views:
    443
    R.F. Pels
    May 22, 2005
  3. Sameer
    Replies:
    4
    Views:
    605
    Roedy Green
    Aug 31, 2005
  4. Uzytkownik
    Replies:
    3
    Views:
    594
    Uzytkownik
    Apr 3, 2005
  5. Øyvind Isaksen
    Replies:
    1
    Views:
    983
    Øyvind Isaksen
    May 18, 2007
Loading...

Share This Page