C# 中单击错误时更新 pictureBox 的计时器

发布于 2024-12-02 14:57:22 字数 505 浏览 5 评论 0原文

我有以下代码 -

    private void button1_Click(object sender, EventArgs e)
    {
        _soundplayer.Play();
        timer1_Tick();

    }


    private void timer1_Tick()
    {
        pictureBox1.Image = imageList1.Images[imgIndex++];

    }

由于某种原因,这会在 Form1.Designer.cs 中返回错误 -

Error 1 No overload for 'timer1_Tick' matches delegate 'System.EventHandler'

单击 button1 时,pictureBox1 中的图像应随着计时器刻度每 2 秒更改一次,但是我可以无法克服这个错误。请指教。

I have the following code-

    private void button1_Click(object sender, EventArgs e)
    {
        _soundplayer.Play();
        timer1_Tick();

    }


    private void timer1_Tick()
    {
        pictureBox1.Image = imageList1.Images[imgIndex++];

    }

For some reason this brings back the error in the Form1.Designer.cs -

Error 1 No overload for 'timer1_Tick' matches delegate 'System.EventHandler'

When button1 is clicked the image in pictureBox1 should change every 2 seconds with the timer tick, however I can't get past this error. Please advise.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江城子 2024-12-09 14:57:22

Tick 事件是 EventHandler 类型的事件。它需要事件处理程序的两个参数:

private void timer1_Tick(object sender, EventArgs e)
{
    pictureBox1.Image = imageList1.Images[imgIndex++];
}

这需要您像这样修改 Click 事件处理程序:

private void button1_Click(object sender, EventArgs e)
{
    _soundplayer.Play();
    timer1_Tick(this, EventArgs.Empty);
}

使用设计器添加事件处理程序可以让您摆脱这样的麻烦。选择计时器,单击“属性”窗口中的闪电图标,然后双击“勾选”。

The Tick event is an event of type EventHandler. It requires two arguments for the event handler:

private void timer1_Tick(object sender, EventArgs e)
{
    pictureBox1.Image = imageList1.Images[imgIndex++];
}

Which requires you to modify the Click event handler like this:

private void button1_Click(object sender, EventArgs e)
{
    _soundplayer.Play();
    timer1_Tick(this, EventArgs.Empty);
}

Using the designer to add event handlers can keep you out of trouble like this. Select the timer, click the lightning bolt icon in the Properties window and double-click Tick.

七堇年 2024-12-09 14:57:22

当您单击按钮时启动计时器。并将计时器间隔设置为 2000 毫秒。 Timer_tick 事件将每 2 秒自动创建一次。

    private void timer1_Tick()
    {
        pictureBox1.Image = imageList1.Images[imgIndex++];

    }       
    private void button1_Click(object sender, EventArgs e)
    {
        _soundplayer.Play();
        timer1_Start();
    }

Start timer, when you clicked on a button. And set timer interval to 2000 milliseconds. Timer_tick event will be created automatically every 2 seconds.

    private void timer1_Tick()
    {
        pictureBox1.Image = imageList1.Images[imgIndex++];

    }       
    private void button1_Click(object sender, EventArgs e)
    {
        _soundplayer.Play();
        timer1_Start();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文