簡體   English   中英

如果我單擊按鈕,計時器將如何遞增而不遞減?

[英]How can i make that if i click the button the timer will count up instead down?

如果我單擊按鈕,標志將為true,並且在timer1 tick事件中,它將更改計數並遞增計數。 如果我再次單擊相同的按鈕,它將倒計時。 在不重置計時器的情況下,僅需保持該計時器根據該標志遞增或遞減即可。 如果為True,則應遞增計數;如果為false,則應遞減計數。 (該標志在表單頂部設置為false)。

現在,它只計數(倒數)。

這是timer1的滴答事件:

private void timer1_Tick(object sender, EventArgs e)
        {
            if (hours == 0 && mins == 0 && secs == 0)
            {
                timer1.Stop();
                MessageBox.Show(new Form() { TopMost = true }, "Times up!!! :P", "Will you press OK? :P", MessageBoxButtons.OK, MessageBoxIcon.Information);
                textBox1.Text = "00";
                textBox2.Text = "00";
                textBox3.Text = "00";
                textBox3.Enabled = true;
                textBox2.Enabled = true;
                textBox1.Enabled = true;
                button1.Enabled = true;
                lblHr.Text = "00";
                lblMin.Text = "00";
                lblSec.Text = "00";
                button2.Enabled = false;
                button3.Enabled = false;
            }
            else
            {
                if (secs < 1)
                {
                    secs = 59;
                    if (mins < 1)
                    {
                        mins = 59;
                        if (hours != 0)0
                            hours -= 1;
                    }
                    else mins -= 1;

                }
                else secs -= 1;
                if (hours > 9)
                    lblHr.Text = hours.ToString();
                else lblHr.Text = "0" + hours.ToString();
                if (mins > 9)
                    lblMin.Text = mins.ToString();
                else lblMin.Text = "0" + mins.ToString();
                if (secs > 9)
                    lblSec.Text = secs.ToString();
                else lblSec.Text = "0" + secs.ToString();
            }
        }

private void button4_Click(object sender, EventArgs e)
        {
            count_up_down = true;
        }

首先,將時間的表示形式從小時,分鍾和秒更改為純seconds 通過將秒數除以60和3600來設置標簽中的文本,如下所示:

int hours = totalSeconds / 3600;
int minutes = totalSeconds / 60;
int seconds = totalSeconds % 60;

添加一個名為step的整數實例變量,並將其設置為負數1

private int step = -1;

單擊按鈕時,更改step變量的符號:

step = -step;

現在,您要做的就是更改代碼以使用totalSeconds += step而不是totalSeconds += step xyz -= 1來完成!

    DateTime start = DateTime.MinValue;
    TimeSpan oldTime = TimeSpan.Parse("00:00:00");
    tm = new Timer();
    tm.Tick += new EventHandler(tm_Tick);

 void tm_Tick(object sender, EventArgs e)
            {
                TimeSpan runTime = DateTime.Now - start;
                lblTimer.Text = string.Format("{1:D2}:{2:D2}:{3:D2}",
                                                runTime.Days,
                                                runTime.Hours,
                                                runTime.Minutes,
                                                runTime.Seconds);

            }

希望以上代碼對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM