Search code examples
c#picturebox

Drawing Circles inside a WinForms picture box


This is what I have done so far. What I need to do is to draw circles with a button using local variables. But I'm not sure how to do it.

public partial class Form1 : Form
{
    Graphics paper;

    public Form1()
    {
        InitializeComponent();
        paper

    }
    //Part One 
    private void btnCircumfance_Click(object sender, EventArgs e)
    {
        double radius;
        if (double.TryParse(txtRadius.Text, out radius))
        {
            radius = 2 * Math.PI * radius;
            radius = Math.Round(radius, 2);
            lblDisplay.Text = "Circumfance is : ";
            lblAnwser.Text = Convert.ToString(radius);
        }
        else
        {
            MessageBox.Show("Please put in a number", "Stupid Human", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }
    }
    //Part One
    private void btnArea_Click(object sender, EventArgs e)
    {

        double radius;
        if (double.TryParse(txtRadius.Text, out radius))
        {
            radius = Math.PI * Math.Pow(radius, 2);
            radius = Math.Round(radius, 2);
            lblDisplay.Text = "Area is : ";
            lblAnwser.Text = Convert.ToString(radius);
        }
        else
        {
            MessageBox.Show("Please put in a number", "Stupid Human", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }
    }
    //Part One
    private void btnVolume_Click(object sender, EventArgs e)
    {
        double radius;
        if (double.TryParse(txtRadius.Text, out radius))
        {
            radius = (4.0/3.0) * Math.PI * Math.Pow(radius, 3);
            radius = Math.Round(radius, 2);
            lblDisplay.Text = "Volume is : ";
            lblAnwser.Text = Convert.ToString(radius);
        }
        else
        {
            MessageBox.Show("Please put in a number", "Stupid Human", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }
    }
    //Part Two
    private void btnRadius_Click(object sender, EventArgs e)
    {
        double Circumfance;
        if (double.TryParse(txtCircumfance.Text, out Circumfance))
        {
            double anwser = 2 * Math.PI;
            Circumfance = Circumfance / anwser;
            Circumfance = Math.Round(Circumfance, 2);
            lblDisplayRadius.Text = "Radius is : ";
            lblAnwserRadius.Text = Convert.ToString(Circumfance);
        }
        else
        {
            MessageBox.Show("Please put in a number", "Stupid Human", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }
    }
    private void btnDraw_Click(object sender, EventArgs e)
    {
        int xCord, yCord;
        int radius;
        xCord = Convert.ToInt16(txtxCord.Text);
        yCord = Convert.ToInt16(txtyCord.Text);
        radius = Convert.ToInt16(txtRadius.Text);

        //Graphics paper = e.Graphics;
        Pen blackPen = new Pen(Color.Black);
        paper.DrawEllipse(blackPen, xCord, yCord, radius, radius); 

        Refresh();
    }
}

Solution

  • Do all drawing in Paint routine.

    First of all, cut all your code out of btnDraw_Click. Instead, in there, just put:
    me.invalidate(true); or similar

    Then create an event handler for the picturebox's 'Paint' event.

    In there, you'll get passed SEnder as object and e as PaintArgs. PaintArgs holds the key.

    Pen blackpen ... 
    _e.Graphics_.DrawEllipse(....
    

    You might have to guard it with an IF statement to not draw until all arguments are answered. Painting will occur when windows wants, not just when the 'draw' button invalidates. You could have the Draw routine transfer the 'paramenters entered' into a new set of parameters that are used from the DrawEllipse if you didn't want them to update the ellipse prior to a click on 'draw'.

    For a nuanced explanation, visit Bob Powell's GDI+ FAQ and click on the top question 'Why do my graphics keep disappearing'?