0

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;
        }
    }
}
DimityrU
  • 11
  • 5
  • Do not set the pbox to null; the way to clear drawing is to Invalidate the control; but we can't know if this works for you as we don't know how you draw. What are your Figures? – TaW May 21 '20 at 16:46
  • [Control.Paint](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.paint?view=netcore-3.1) –  May 21 '20 at 16:52
  • Note that there are two different, recommended ways to create graphics: [drawing onto a control or into a bitmap](https://stackoverflow.com/questions/27337825/picturebox-paintevent-with-other-method/27341797?r=SearchResults&s=2|20.9708#27341797) - The former is for dynamic the latter for more static drawing. – TaW May 21 '20 at 16:57
  • @TaW Tried .Referesh, but did not work. – DimityrU May 22 '20 at 14:22
  • Did you understand anything in my comments?? We still don't know what Figure is nor where the Graphics object comes from nor whether you draw onto a control or into a bitmap. – TaW May 22 '20 at 15:03
  • @TaW I added class for Figure and Rectangle. – DimityrU May 22 '20 at 16:21
  • 2
    You clear the canvas and draw everything again, omitting what you don't want drawn this time. – LarsTech May 22 '20 at 16:22
  • @LarsTech Sorry, didn't understand you – DimityrU May 22 '20 at 16:25
  • 1
    To "delete a shape", you have to start over with a blank graphic, loop through your figures, and "not" draw the one you want erased. Graphics are fire and forget, so once it's drawn, there isn't a mechanism to erase it. – LarsTech May 22 '20 at 16:27
  • OK. Your clearing code misses a few points. a) you need to clear the bitmap by creating it again or by filling it with a background color. b) you need to re-assign that cleared image to the pbox.Image as it will not pick up any changes otherwise. So instead of `pictureBox = null;` write e.g. `draw.Clear(someColor); pictureBox.Image = bitmap;` - Also: Your choice of names it confusing, to say the least.. – TaW May 22 '20 at 17:09
  • @TaW It works. Not with KeyEvent, but with new button. Any ideas why? – DimityrU May 22 '20 at 21:53
  • No. Does it hit the updated(?) code in the if clause? – TaW May 22 '20 at 23:19
  • Add it above. Code in KeyEvent and in the button are same. – DimityrU May 23 '20 at 06:23

0 Answers0