René Beneš
René Beneš

Reputation: 468

c# Panel line overwriting

Inside the Paint event for panel, I have a code, that should draw a blue line between 2 points.

        private void panel1_Paint(object sender, PaintEventArgs e)
    {
        panel1.AllowDrop = true;
        listBox1.AllowDrop = true;
        if (!s.IsEmpty && !f.IsEmpty)
        {

            e.Graphics.DrawLine(new Pen(Color.Blue, 5f), f, s);
            s = Point.Empty;
            f = Point.Empty;

        }

    }

Im Invalidating the panel in SetPoint method:

     void setPoint(Point p)
    {
        if (f.IsEmpty)
            f = p;
        else
        {
            s = p;
            panel1.Invalidate();
        }


    }

That is triggered by clicking on a button. It will draw a line, but the problem is, that when one line already exists. It will overwrite it. I thought problem is in Invalidate. But I dont know how to fix it because Refresh() or Update() Doesn't work.
What I'm doing wrong?

Upvotes: 0

Views: 231

Answers (1)

geniaz1
geniaz1

Reputation: 1183

You need to save all your points in some structure. In Paint method you will loop through the structure and draw all the lines.

That's because when Paint is activated it redraws all the control once again, and it can't "remember" what was there before, it is only doing what you wrote inside.

Upvotes: 1

Related Questions