I draw the shapes and visualize them on Windows Form (wiith PictureBox) and add them to List<> , but I can't delete them from picture box with KeyEvent Tried to clear the List and picturebox but didn't work Any suggestions???
This is the class for Rectangle
public class Rectangles : Figure
{
public Rectangles(int A,int B,int C,int D) : base(A, B, C, D)
{ }
public override void Draw(Graphics draw)
{
using(var brush = new SolidBrush(
Color.FromArgb(
Math.Min(byte.MaxValue, Color.R + 100),
Math.Min(byte.MaxValue, Color.G + 100),
Math.Min(byte.MaxValue, Color.B + 255))))
draw.FillRectangle(brush, A, B, C, D);
using (Pen Pen = new Pen(Color.Blue, 2))
{
draw.DrawRectangle(Pen, A, B, C, D);
}
}
}
This is the class for Shapes
public abstract class Figure
{
public Color Color { get;protected set; }
public int A { get;protected set; }
public int B { get;protected set; }
public int C { get;protected set; }
public int D { get;protected set; }
protected Figure(int A, int B, int C, int D)
{
this.A = A;
this.B = B;
this.C = C;
this.D = D;
}
public abstract void Draw(Graphics draw);
}
And the is the class for Form
public partial class Scene : Form
{
public Bitmap bitmap;
public Graphics draw;
public List<Rectangles> drawRectangle = new List<Rectangles>();
public Squares drawSquare;
public Triangles drawTriangle;
public Circles drawCircle;
public List<Figure> Figures { get; set; }
public Scene()
{
InitializeComponent();
Figures = new List<Figure>();
}
public void ShowBox()
{
bitmap = new Bitmap(pictureBox.Height, pictureBox.Width);
draw = Graphics.FromImage(bitmap);
pictureBox.Image = bitmap;
}
private void button1_Click(object sender, EventArgs e)
{
ShowBox();
if (rectangleBox.Checked == true)
{
var drawRectangle = new Rectangles(50 + int.Parse(side_a.Text), 50 + int.Parse(side_b.Text),
50 + int.Parse(side_a.Text), 50 + int.Parse(side_b.Text));
Figures.Add(drawRectangle);
};
foreach(Figure figure in Figures)
{
figure.Draw(draw);
}
}
private void button2_Click(object sender, EventArgs e)
{
Figures.Clear();
draw.Clear(Color.WhiteSmoke);
pictureBox.Image = bitmap;
}
private void Scene_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
Figures.Clear();
draw.Clear(Color.WhiteSmoke);
pictureBox.Image = bitmap;
}
}
}