2

Actually there are some similar questions about this topic but I couldn't see the answer what I am looking for.

For example I have drawn 2 lines on windows form and I want to delete one of them and keep the other, how can I do that?

this.Invalidate(); or Graphics.Clear(); clear all the form, I don't want this, I want to delete specific line. Do you have any other solutions?

braX
  • 11,506
  • 5
  • 20
  • 33
etly
  • 31
  • 1
  • 1
  • 2
  • 3
    Don't use CreateGraphics!! – Hans Passant Jun 19 '16 at 15:26
  • 1
    The only way is to delete the line you want to delete from the __list of lines__ you draw. Always draw all shapes in one go in the paint event. If you don't have such a list of shapes you want to draw, then you should have. You need it. Believe us. Ignore all the stupid advice that says other wise, from MSDN's awful intro on drawing to, sorry to say, the misleading analogy to 'a line drawn on paper'. This is not at all similar. In fact it is totally different!!! – TaW Jun 19 '16 at 16:49
  • 1
    You may want to study [this post](http://stackoverflow.com/questions/32408229/select-drawn-figure-within-panel-box/32422295#32422295). - The short answer: You do not __have__ lines, you just color pixels. And: __You cannot undraw a pixel__ so you need to recreate the whole drawing. Sounds crazy and wasteful but is the only way to go and actually works blindingly fast. – TaW Jun 19 '16 at 17:14
  • When you're drawing your line, add it to a list. When you want to undo, redraw the line using the same color as the background of your container from your list. – Alex Diamond Jun 19 '16 at 17:49
  • Thanks for your advises, giving reference post and example code @HansPassant, Taw and Alex Diamond I will refresh my code in the light of these comments. Thank you! – etly Jun 21 '16 at 07:55

1 Answers1

3

The following will delete the all created lines in reverse chronological sequence.

    Graphics g;
    Pen p;
    Bitmap bmp;
    List<Point> Lines = new List<Point>();

    private void Form2_Load(object sender, EventArgs e)
    {
        bmp = new Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
        BackgroundImage = bmp;
        g = Graphics.FromImage(BackgroundImage);
        g.Clear(Color.DeepSkyBlue); //This is our backcolor
    }

    private void btnLine1_Click(object sender, EventArgs e)
    {
        Point A = new Point(50, 50);
        Point B = new Point(100, 50);
        p = new Pen(Color.Red);
        g.DrawLine(p, A, B); //Use whatever method to draw your line
        Lines.Add(A); //Grab the first point; add to list
        Lines.Add(B); //Grab the second point; add to list
        Refresh(); //Refresh drawing to bitmap.
    }

    private void btnDrawLine2_Click(object sender, EventArgs e)
    {
        Point A = new Point(50, 60);
        Point B = new Point(100, 60);
        p = new Pen(Color.White);
        g.DrawLine(p, A, B); //Same logic as above
        Lines.Add(A);
        Lines.Add(B);
        Refresh();
    }

    private void btnUndo_Click(object sender, EventArgs e)
    {
        c = new Pen(Color.DeepSkyBlue);
        r = new Pen(lastColor.ElementAt(lastColor.Count - 2));
        try
        {
            g.DrawLine(c, Lines.ElementAt(Lines.Count - 2), Lines.ElementAt(Lines.Count - 1));
            Lines.RemoveAt(Lines.Count - 2);
            Lines.RemoveAt(Lines.Count - 1);
            for (int i = Lines.Count; i > 0; i--)
            {
            g.DrawLine(r, Lines.ElementAt(Lines.Count - 2), Lines.ElementAt(Lines.Count - 1));
            }
        }
        catch { }
        Refresh();
    }

Here's 2 lines side by side:

Result1

Here's 2 lines overlapping:

Result2

*Remember to dispose your graphics objects!

Alex Diamond
  • 586
  • 3
  • 27
  • Draw one line crossing another and watch it remove bits of the underlying line as well. – Lasse V. Karlsen Jun 19 '16 at 18:50
  • Just make one function responsible for all drawing, using the view model. Then the buttons for adding new lines don't need to draw anything, they just update the view model. What you have now is a lot of duplication which is almost impossible to maintain. – Ben Voigt Jun 19 '16 at 19:21
  • Actually I don't know anything about view model now but I will search about it! Thank you for your advice Ben Voigt. – etly Jun 21 '16 at 07:57
  • `.new Pen(lastColor.ElementAt(lastColor.Count - 2));` Where is lastColor from? This isn't working. – suchislife May 13 '19 at 02:38