0

I was working a program where a rectangle is drawn inside a label through a paint event. Then with a button click event I want to redraw the inside of label now having the original rectangle and a new one that is .TranslateTransform some some input. Now to the problem: I was able to create the new original and the copy but if I press the button then I get a copy of the copy. I thought I could fix this if before the redrawing I use label.Refresh() or .Invalidate() but now I can't even get a single copy.

Thanks in advance for the help. Here is the code:

 public int x, y;
 public float p, q;
 Graphics g;
 SolidBrush brush1 = new SolidBrush(Color.Green);

        public Form1()
        {
            InitializeComponent();

        }

        private void label5_Paint_1(object sender, PaintEventArgs e)
        {
            g = e.Graphics;

            g.FillRectangle(brush1, 50, 50, 100, 50);

        }

        private void shift(object sender, PaintEventArgs e)
        {
            string a, b;
            a = textBox1.Text;
            x = int.Parse(a);
            b = textBox2.Text;
            y = int.Parse(b);

            var g = e.Graphics;
            Brush br = new SolidBrush(Color.DarkOrange);
            g.TranslateTransform(x, y);
            g.FillRectangle(br, 50, 50, 100, 50);
        }

        private void button1_Click(object sender, EventArgs e)
        {

            label5.Invalidate();
            label5.Paint += new PaintEventHandler(shift);

        }

        private void resize(object sender, PaintEventArgs e)
        {
            string c, d;
            c = textBox3.Text;
            p = float.Parse(c);
            d = textBox4.Text;
            q = float.Parse(d);



            var g = e.Graphics;
            Brush br = new SolidBrush(Color.DarkOrange);

            g.ScaleTransform(p, q);
            g.FillRectangle(br, 50, 50, 100, 50);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            label5.Invalidate();

            label5.Paint += new PaintEventHandler(resize);

        }
Esra
  • 11
  • 1
    You need to use a the `PaintEventArgs` object provided in a Control's Paint event to draw your Graphics. All of them. Don't use `Control.CreateGraphics()` for this and, more important, don't store a Graphics object: it won't be a valid object as soon as the Control is invalidated. See the simple example [here](https://stackoverflow.com/a/53708936/7444103), to draw different shapes and keep track of them. – Jimi May 12 '20 at 17:16
  • Note that calling refresh or invalidate will trigger the Paint event. Also: each time you press one of your buttons you add another event to the paint queue which will be called from now on. Hardly what you want.. - Also: The Tranform calls will change the Graphics object until you Reset it or another Paint call happens. – TaW May 12 '20 at 17:22
  • It's not clear what this is: `label5.Paint += new PaintEventHandler(resize);` and this `label5.Paint += new PaintEventHandler(shift);` are meant to accomplish. More event handlers to draw more shapes? That's not how it works. – Jimi May 12 '20 at 17:23
  • @Jimi Thanks for the example. Regarding the `new PaintEventHandler(shift)` it was the only way I could think of to active the paint event of the label with the button click event. I tried to just call `label5_Paint_1(object,e)` inside the button click event but it won't allow me because the click event is `EventArgs` and the label5 is `PaintEventArgs` – Esra May 13 '20 at 00:02
  • When you want to raise the Paint event, you call `Control.Invalidate()`. – Jimi May 13 '20 at 06:05

0 Answers0