0

I have this code:

private void Form1_MouseWheel(object sender, MouseEventArgs e)
        {
            if (leave == true)
            {
                if (e.Delta > 0)
                {
                    if (timer1.Interval < 5000)
                    {
                        timer1.Interval += 1000;
                        label2.Text = (timer1.Interval/1000).ToString();
                    }

                }
                else
                {
                    if (timer1.Interval == 1000)
                    {
                        timer1.Interval -= 100;
                        label2.Text = (timer1.Interval / 1000).ToString();
                    }
                }
            }
        }

The original timer1 interval in the designer is set to 1000 milliseconds. In the mouse wheel event i did that it will show in the label2 the untis in seconds. And indeed when i move the mouse wheel up its slowing the timer and show it in seconds 1 2 3 4 5

The problem is with the second part i wanted that when its getting to 1 second or 1000 milliseconds if i will keep wheel it down it will show the units in 100 and change the timer1.interval in 100 units.

So in label2 if it was on 1 second so now i will see 900 800 700 600 500 un till 100. And also the timer1 interval should be change to 900 milliseconds 800 700 600 untill 100.

When its get to 100 just stop there dont keep getting under 100.

The problem is with this part:

if (timer1.Interval == 1000)
                    {
                        timer1.Interval -= 100;
                        label2.Text = (timer1.Interval / 1000).ToString();
                    }

Its not working at all.

EDIT**

My code now:

if (leave == true)
            {
                if (e.Delta > 0)
                {
                    if (timer1.Interval < 5000)
                    {
                        timer1.Interval += 1000;
                        label2.Text = (timer1.Interval / 1000).ToString();
                    }

                }
                else
                {
                     if (timer1.Interval > 1000) {
                         timer1.Interval -= 1000;
                         label2.Text = (timer1.Interval / 1000).ToString();
                     }

                    else

                    if (timer1.Interval <= 1000 && timer1.Interval > 100)
                    {
                        timer1.Interval -= 100;
                        label2.Text = (timer1.Interval / (double)1000).ToString();
                    }
                }
            }

But now if i was at 5 seconds(5000 milliseconds) now i move the wheel back down its counting 5 4 3 2 1 0 and stop on 0

It dosent show under 1 ...0.9 0.8 0.7 as it did before.

4

1 回答 1

1

您应该将检查更改为:

if (timer1.Interval <= 1000 )

计时器间隔只会是1000一次,在您从中减去 100 并使其变为 900 之前。通过检查值<= 1000,它将继续为较低的值工作。

您可能还想检查间隔是否太低并将检查扩展为

if (timer1.Interval <= 1000 && timer1.Interval > 100)

要显示小于 1 秒的时间,您需要确保除以浮点类型,否则只会显示 0。除以整数会导致任何分数被截断

label2.Text = (timer1.Interval / (double)1000).ToString();

要减少Interval高于 1000 的值,您需要进行额外检查。例如

if (timer1.Interval > 1000) {
    timer.Interval -= 1000;
} else if ( timer.Interval <= 1000 && timer.Interval > 100 )
    timer.Interval -= 100;
}
于 2013-10-24T21:17:14.117 回答