In my code, I have a PictureBox
with a background picture. I used to draw a rectangle over it using
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Pen p = new Pen(Color.Turquoise, 2);
Rectangle r = new Rectangle(600, 300, 5, 5);
e.Graphics.DrawRectangle(p, r);
p.Dispose();
}
Now, that I know I will need to do a lot of things with these rectangles and create them dynamically, I have created a class for them, with a constructor looking like this:
public MyRectangles(int x, int y)
{
Pen p = new Pen(Color.Turquoise, 2);
Rectangle r = new Rectangle(x, y, 5, 5);
e.Graphics.DrawRectangle(p, r);
p.Dispose();
}
The problem is, that the e
in e.Graphics.DrawRectangle(p, r);
does not exist here. It makes sense, I understand why, however, I dont know what to replace it with, to draw on the same picturebox again.
Try passing the Graphics object:
public MyRectangles(Graphics g, int x, int y)
{
Pen p = new Pen(Color.Turquoise, 2);
Rectangle r = new Rectangle(x, y, 5, 5);
g.DrawRectangle(p, r);
p.Dispose();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With