Share via


Draw a circle in Winforms C#

Question

Sunday, August 16, 2020 8:46 AM

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

All replies (3)

Sunday, August 16, 2020 2:30 PM âś…Answered

You can just call Graphics.DrawEllipse,

 for example in the Paint event of a PictureBox


Sunday, August 16, 2020 2:43 PM

But how can i change color of that ellipse when i press a button?


Sunday, August 16, 2020 3:23 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();