-2

So I am trying to make a red ellipse flash in the middle but for some reason my codes runs and complies but no ellipse is drawn

As you can see in the code part I have put the drawing part in the default constructor, I have also tried creating a private function that draws it and tried calling it in the default constructor but that didn't work either

using System;
using System.Drawing;
using System.Windows.Forms;

public class RedLuserinterface : Form
{
    private Panel panelTop = new Panel();
    private Panel panelMid = new Panel();
    private Panel panelBot = new Panel();
    private Label title = new Label();
    private Button pauseBut = new Button();
    private Button resumeBut = new Button();
    private Button exitBut = new Button();
    private Size minInterfaceSize = new Size(400, 600);
    private Size maxInterfaceSize = new Size(400, 600);

    public RedLuserinterface()
    {   //Set the size of the user interface box.
        MaximumSize = minInterfaceSize;
        MinimumSize = maxInterfaceSize;
        //Initialize text strings
        Text = "Red Light Assignment";
        title.Text = "Red Light Program";
        pauseBut.Text = "Pause";
        resumeBut.Text = "Resume";
        exitBut.Text = "Exit";

        //Set Sizes
        Size = new Size(400, 600);
        panelTop.Size = new Size(400, 30);
        panelMid.Size = new Size(400, 160);
        panelBot.Size = new Size(400, 50);
        title.Size = new Size(120, 30);
        pauseBut.Size = new Size(85, 30);
        resumeBut.Size = new Size(85, 30);
        exitBut.Size = new Size(85, 30);

        //Set Locations
        title.Location = new Point(140, 20);
        panelTop.Location = new Point(0, 0);
        panelMid.Location = new Point(0, 30);
        panelBot.Location = new Point(0, 480);
        pauseBut.Location = new Point(50, 500);
        resumeBut.Location = new Point(40, 150);
        exitBut.Location = new Point(250, 500);

        //Add controls to the form
        Controls.Add(title);
        Controls.Add(panelTop);
        Controls.Add(panelMid);
        Controls.Add(panelBot);
        Controls.Add(pauseBut);
        Controls.Add(resumeBut);
        Controls.Add(exitBut);

        //Set Color
        panelTop.BackColor = Color.Green;
        panelMid.BackColor = Color.Blue;
        panelBot.BackColor = Color.Yellow;

        //Create solid brush and draw ellipse
        SolidBrush redBrush = new SolidBrush(Color.Red);
        Graphics circle = this.CreateGraphics();
        circle.FillEllipse(redBrush, 0, 0, 200, 200);

        //send some stuff to the back
        panelTop.SendToBack();
        panelMid.SendToBack();
        panelBot.SendToBack();

        pauseBut.Enabled = true;
        resumeBut.Enabled = false;


        exitBut.Click += new EventHandler(stoprun);
        //dispose stuff
        redBrush.Dispose();
        circle.Dispose();
    }
}
JSteward
  • 6,833
  • 2
  • 21
  • 30
  • so create a function you mean? – Ernest Lin Aug 30 '19 at 21:41
  • 1
    _Graphics circle = this.CreateGraphics();_ __Winforms graphics basic rule #1__ : Never use `control.CreateGraphics`! Never try to cache a `Graphics` object! Either draw into a `Bitmap bmp` using a `Graphics g = Graphics.FromImage(bmp)` or in the `Paint` event of a control, using the `e.Graphics` parameter.. – TaW Aug 31 '19 at 06:21

1 Answers1

0

You need to paint your ellipse when the Form engine requires you to do it. The form engine will call the Paint event handler if you define one and will pass the Graphics object to use to paint the ellipse. So you should remove the lines in the form constructor and add the proper delegate for the Paint event, then in the Paint event draw the ellipse

public RedLuserinterface()
{   
    .....
    // Remove these lines 
    // SolidBrush redBrush = new SolidBrush(Color.Red);
    // Graphics circle = this.CreateGraphics();
    // circle.FillEllipse(redBrush, 0, 0, 200, 200)

    .... 
    exitBut.Click += new EventHandler(stoprun);
    this.Paint += onFormPaint;

    // No more needed here
    // redBrush.Dispose(); 
    // circle.Dispose()
}
private void onFormPain(object sender, PaintEventArgs e)
{
    SolidBrush redBrush = new SolidBrush(Color.Red);
    e.Graphics.FillEllipse(redBrush, 50,250, 200, 200);
    redBrush.Dispose();
}
Steve
  • 213,761
  • 22
  • 232
  • 286
  • what's with the +=? – Ernest Lin Aug 30 '19 at 22:05
  • Both _this.Paint_ and _exitBut.Click_ use this operator to assign the code that should be executed when the specific event is raised by the form engine. The second form (only += without new EventHandler) is a shortcut available in latest releases of the C# compiler. In Microsoft Docs this is called "subscribing to event" See here https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/events/how-to-subscribe-to-and-unsubscribe-from-events – Steve Aug 31 '19 at 07:01