Roberts Šensters
Roberts Šensters

Reputation: 605

How to call 'draw circle' from method to draw inside picturebox

I don't know what to pass to my method to call my Circle drawing properly. Here is my code:

    private void Circle()
    {
        Graphics g1 = e.Graphics;
        Pen p1 = new Pen(Color.Black);
        g1.DrawEllipse(p1, 12, 12, 50, 50);
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        Circle();
    }

This is not working , cause there is no 'e'.

If i rewrite my code from

Graphics g1 = e.Graphics;

to

Graphics g1 = this.CreateGraphics();

It will draw circle but not in the picturebox. I need a circle to be inside picturebox.

Upvotes: 1

Views: 569

Answers (2)

guntbert
guntbert

Reputation: 536

To get a reference to a Graphics object you can provide that via the parameters. Like

private void Circle(Graphics g)
{
    Pen p1 = new Pen(Color.Black);
    g.DrawEllipse(p1, 12, 12, 50, 50);
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    Graphics myg = e.Graphics;
    Circle(myg);
}

Upvotes: 1

David
David

Reputation: 218798

If your method requires a reference to the PaintEventArgs, then why not supply it one? Something like this:

private void Circle(PaintEventArgs e)
{
    Graphics g1 = e.Graphics;
    Pen p1 = new Pen(Color.Black);
    g1.DrawEllipse(p1, 12, 12, 50, 50);
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    Circle(e);
}

This would allow other paint event handlers to make use of that method for their own controls as well.

Upvotes: 5

Related Questions