Reputation: 2392
I'm currently working on a user control in C # Winforms, which allows you to add a figure with properties each time from the designer with, so that you understand me better, I'll take a reference to the DataGridView, that every time a column is added, it has several parameters: Title, text alignment, etc, all within each object Column
Well, in my case I have this class:
public class Shape
{
public int X { get; set; }
public int Y { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public ShapeType ShapeType { get; set; }
public Color Color { get; set; }
public bool Mirrored { get; set; }
}
Well, my goal is to create a collection of Shape objects where in each Shape object their properties are encapsulated:
private List<Shape> shapelist = new List<Shape>();
public List<Shape> Shapes
{
get => this.shapelist;
set => this.shapelist = value;
}
Where do I add parameters when instantiating control from the Windows Forms designer:
public ShapesDecoration()
{
InitializeComponent();
Init();
}
public void Init()
{
Shapes[0] = new Shape();
Shapes[0].Width = 148;
Shapes[0].Height = 64;
Shapes[0].X = 20;
Shapes[0].Y = 20;
Shapes[0].Color = Color.DodgerBlue;
Shapes[0].ShapeType = ShapeType.Circle;
}
To then call these parameters of each object in the list, for example, here I am calling the parameters of the Shape object in the first position:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Hector.Framework.Utils.ShapeCollection.FillCircle(e, Shapes[0].Color, Shapes[0].X, Shapes[0].Y, Shapes[0].Width, Shapes[0].Height);
}
My problem is that every time I drag the control to the form, Visual Studio shows me an error message which says that Shape is not marked as serializable
So, lists do not support classes like data types?
Or am I implementing something wrongly?
How can I create a collection of the Shape object, each object with its respective parameters?
Upvotes: 1
Views: 1093
Reputation: 24913
Simple add attribute:
[Serializable]
public class Shape
{
public int X { get; set; }
public int Y { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public ShapeType ShapeType { get; set; }
public Color Color { get; set; }
public bool Mirrored { get; set; }
}
Upvotes: 1