开发者

Timer updating pictureBox on click error in C#

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 chang开发者_运维知识库e every 2 seconds with the timer tick, however I can't get past this error. Please advise.


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.


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();
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜