如何使用计时器使椭圆闪烁。C#上的间隔?(紧急红灯模拟)(在Ubuntu上使用MonoDevelop)

如何使用计时器使椭圆闪烁。C#上的间隔?(紧急红灯模拟)(在Ubuntu上使用MonoDevelop),c#,events,event-handling,traffic,event-driven,C#,Events,Event Handling,Traffic,Event Driven,我不知道如何让我的代码上的红灯按照我为它设置的时间间隔闪烁 我尝试过使用Invalidate和Update以及Refresh,但它们似乎都不起作用。我对C#和一般的编码非常陌生,所以代码可能有严重的缺陷,如果是这样的话,请毫不犹豫地对我大喊大叫 public class UI: Form { protected Label lights_title = new Label(); protected Button pause, resume, exit; protected

我不知道如何让我的代码上的红灯按照我为它设置的时间间隔闪烁

我尝试过使用Invalidate和Update以及Refresh,但它们似乎都不起作用。我对C#和一般的编码非常陌生,所以代码可能有严重的缺陷,如果是这样的话,请毫不犹豫地对我大喊大叫

public class UI: Form
{
    protected Label lights_title = new Label();
    protected Button pause, resume, exit;
    protected bool isPaint = true;
    public static System.Timers.Timer g_shock = new System.Timers.Timer();

    public UI()
    {
        // text initialization
        this.Text = "Chukwudi's Flashing Red Light";
        this.lights_title.Text = "Ikem's Emergency Lights!";
        // size initialization - determine the size of the UI Form
        this.lights_title.Size = new Size(700, 40);
        this.Size = new Size(1920, 1080);

        // location initialization (x,y) x pulls you right as the number increase, y pulls you down
        this.lights_title.Location = new Point(598, 60);

        // title config & background color
        this.lights_title.BackColor = Color.Coral;
        this.lights_title.TextAlign = (System.Drawing.ContentAlignment)HorizontalAlignment.Center;

        this.BackColor = Color.DimGray;

        Render();

        g_shock.Enabled = false;
        g_shock.Elapsed += ManageOnPaint;
        g_shock.Enabled = true;
        g_shock.Start();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics shape = e.Graphics;
        if (isPaint)
        {
            shape.FillRectangle(Brushes.Black, new Rectangle(598, 90, 700, 700));
            shape.FillEllipse(Brushes.Red, new Rectangle(650, 140, 600, 600));
        }
        shape.FillRectangle(Brushes.Brown, new Rectangle(598, 785, 700, 60));
    }

    protected void ManageOnPaint(object sender, System.Timers.ElapsedEventArgs elapsed)
    {
        g_shock.Interval = 1000;
        Invalidate();
        Update();
        // turn something to true
    }
}

正如Alex F所指出的,您并不是通过切换某些东西来跟踪控件是否为红色。 你在《时代周刊》里什么也没画。 我的建议如下,我遗漏了一些与我的观点无关的内容

private bool isRed;
private static System.Timers.Timer g_shock=新系统.Timers.Timer();
公共用户界面()
{
g_shock.appead+=管理油漆;
g_冲击间隔=1000;
g_shock.Start();
}
私有void ManageOnPaint(对象发送器,System.Timers.ElapsedEventArgs)
{
如果(isRed)
{
//将椭圆颜色设置为非红色
}
如果(!isRed)
{
//设置椭圆颜色为红色
}
//切换isRed
isRed=!isRed;
}
受保护的覆盖无效OnPaint(PaintEventArgs e)
{
///使用当前椭圆颜色绘制椭圆
}

正如Alex F所指出的,您没有通过切换某些东西来跟踪控件是否为红色。 你在《时代周刊》里什么也没画。 我的建议如下,我遗漏了一些与我的观点无关的内容

private bool isRed;
private static System.Timers.Timer g_shock=新系统.Timers.Timer();
公共用户界面()
{
g_shock.appead+=管理油漆;
g_冲击间隔=1000;
g_shock.Start();
}
私有void ManageOnPaint(对象发送器,System.Timers.ElapsedEventArgs)
{
如果(isRed)
{
//将椭圆颜色设置为非红色
}
如果(!isRed)
{
//设置椭圆颜色为红色
}
//切换isRed
isRed=!isRed;
}
受保护的覆盖无效OnPaint(PaintEventArgs e)
{
///使用当前椭圆颜色绘制椭圆
}

我很困惑。如何让它以非OnPaint的方法进行绘制?我是否将PaintEvent传递给ManageOnPaint?不管怎样,我理解您在这里所做的工作,并将其实现到我自己的代码中。我抓到你了。谢谢很高兴它帮助了你@Chukwudi我很困惑。如何让它以非OnPaint的方法进行绘制?我是否将PaintEvent传递给ManageOnPaint?不管怎样,我理解您在这里所做的工作,并将其实现到我自己的代码中。我抓到你了。谢谢很高兴它帮助了你@Chukwudi