Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 定时器。滴答不响';行不通_C#_Winforms_Timer - Fatal编程技术网

C# 定时器。滴答不响';行不通

C# 定时器。滴答不响';行不通,c#,winforms,timer,C#,Winforms,Timer,我正在尝试使用timer.tick移动面板上的图像,但由于某些原因它无法工作。。。 在“属性”窗口中启用计时器,这是我的代码: public partial class Form1 : Form { private Image img; private Point pos; public Form1() { InitializeComponent(); img = Image.FromFile("C:/object.png");

我正在尝试使用timer.tick移动面板上的图像,但由于某些原因它无法工作。。。 在“属性”窗口中启用计时器,这是我的代码:

public partial class Form1 : Form
{
    private Image img;
    private Point pos;

    public Form1()
    {
        InitializeComponent();

        img = Image.FromFile("C:/object.png");
        pos = new Point(100, 100);
    }

    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.DrawImage(img, pos);
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        pos.X += 10;
        pos.Y += 10;

        Invalidate();
    }
}
我看不出这里有什么问题??)

我也试过:

public partial class Form1 : Form
{
    private Image img;
    private Point pos;

    public Form1()
    {
        InitializeComponent();

        img = Image.FromFile("C:/object.png");
        pos = new Point(100, 100);

        Timer myTimer = new Timer();
        myTimer.Enabled = true; // don't know if this goes here
        myTimer.Tick += new EventHandler(MoveImg);
        myTimer.Interval = 100;
        myTimer.Start();
    }

    private void MoveImg(Object myObject, EventArgs myEventArgs)
    {
        pos.X += 10;
        pos.Y += 10;

        Invalidate();
    }

    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.DrawImage(img, pos);
    }
}

您需要使用启动计时器

正如汉斯·帕桑(Hans Passant)所说,你必须使正确的对象无效

private void MoveImg(Object myObject, EventArgs myEventArgs)
{
    pos.X += 10;
    pos.Y += 10;

    panel1.Invalidate();
}

您需要使用启动计时器

正如汉斯·帕桑(Hans Passant)所说,你必须使正确的对象无效

private void MoveImg(Object myObject, EventArgs myEventArgs)
{
    pos.X += 10;
    pos.Y += 10;

    panel1.Invalidate();
}

不,我还是不行。我的代码是来自Youtube的一步一步的例子,它在那里以某种方式为所有ppl工作。。。我会添加一些我尝试过的东西。不,对我来说仍然不起作用。我的代码是来自Youtube的一步一步的例子,它在那里以某种方式为所有ppl工作。。。我将添加我尝试过的其他内容。您正在使错误的对象无效,请使用panel1。Invalidate()有效,thnks!!!但只适用于第二个代码…?WPF!=WinFormsYou正在使错误的对象无效,请使用panel1。Invalidate()有效,谢谢!!!但只适用于第二个代码…?WPF!=桌面应用程序