C# 绘制事件没有反应

C# 绘制事件没有反应,c#,winforms,C#,Winforms,我感到绝望,有人有没有想法,为什么我不能发射油漆事件?我尝试了所有三种方法:无效、更新、刷新,但都不起作用 这是我整个超级简单的WinForm项目 using System; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { public System.Windows.Forms.Timer movementTimer;

我感到绝望,有人有没有想法,为什么我不能发射油漆事件?我尝试了所有三种方法:无效、更新、刷新,但都不起作用

这是我整个超级简单的WinForm项目

using System;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public System.Windows.Forms.Timer movementTimer;

        public Form1()
        {
            movementTimer = new System.Windows.Forms.Timer();
            movementTimer.Interval = 10;
            movementTimer.Tick += tick;
            movementTimer.Start();
            this.Invalidate();
            this.Update();
            this.Refresh();
        }

        void tick(object sender, EventArgs e)
        {            
            this.Invalidate();
            this.Update();
            this.Refresh();
        }        

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            // I cannot hit breakpoint here.
            Console.WriteLine("PAINT METHOD HAS BEEN EXECUTED");
        }
    }
}

您在代码的其余部分破坏了一些您没有显示的内容。在空项目上尝试以下操作,它将按预期工作:

public partial class Form1 : Form
{
    Timer timer = new Timer { Interval = 10 };
    public Form1()
    {
        InitializeComponent();
        Paint += (s, e) => { };
        timer.Tick += (s, e) => Refresh();
        timer.Start();
    }
}

这是你的密码。。。有些变化

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Painter
{
    public partial class Form2 : Form
    {
        public System.Windows.Forms.Timer movementTimer;

        public Form2()
        {
            InitializeComponent();

            movementTimer = new System.Windows.Forms.Timer();
            movementTimer.Interval = 10;
            movementTimer.Tick += tick;
            movementTimer.Start();
            this.Invalidate();
            this.Update();
            this.Refresh();
        }

        void tick(object sender, EventArgs e)
        {
            this.Invalidate();
            this.Update();
            this.Refresh();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            // I cannot hit breakpoint here.
            Console.WriteLine("PAINT METHOD HAS BEEN EXECUTED");
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // Form2
            // 
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Name = "Form2";
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
            this.ResumeLayout(false);

        }

    }
}

表格上有吗InitializeComponent方法到哪里去了?现在它可以工作了:-InitializeComponent丢失。非常感谢。我发布了所有的项目。您看到了所有的代码。那么InitializeComponent方法去了哪里?现在它可以工作了:-InitializeComponent丢失了。非常感谢。为什么要调用Invalidate、Update和Refresh?顺便说一句,这在构造函数中没有任何作用,因为表单还不可见。表单不必订阅自己的事件。只需使用OnPaint覆盖。伙计。。。我只是复制了ops代码并让它工作。。。我没有改变他写的任何东西。