locked
Draw a circle in Winforms C# RRS feed

  • Question

  • Hey!

    Im wondering if its possible to create a filled circle here:


    And be able to change color of it if a statement is true?
    Best Regards
    Sunday, August 16, 2020 8:46 AM

Answers

All replies

  • You can just call Graphics.DrawEllipse,

     for example in the Paint event of a PictureBox

    • Marked as answer by Carlo Goretti Sunday, August 16, 2020 4:57 PM
    Sunday, August 16, 2020 2:30 PM
  • But how can i change color of that ellipse when i press a button?
    Sunday, August 16, 2020 2:43 PM
  • For example, with a boolean for red/green ellipse (FillEllipse for filled ellipse) =>

    private bool bOK = false;
    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        Rectangle rect = new Rectangle(0, 0, pictureBox1.ClientSize.Width - 1, pictureBox1.ClientSize.Height - 1);
        System.Drawing.Color color = (bOK ? System.Drawing.Color.Green : System.Drawing.Color.Red);
        using (System.Drawing.Brush brush = new SolidBrush(color))
        {
            e.Graphics.FillEllipse(brush, rect);
        }           
    }
    and in the click event of a button =>

    bOK = !bOK;
    pictureBox1.Invalidate();

    Sunday, August 16, 2020 3:23 PM