Display time elapsed

六眼飞鱼酱① 提交于 2019-12-20 04:25:24

问题


I need to display the elapsed time dynamically. My code will pop up a message based on an interval value.

public void button1_Click(object sender, EventArgs e)
{
    this.TopMost = true;
    DialogResult result1 = MessageBox.Show("Add some notes to your current ticket?",
    "Add Notes",
    MessageBoxButtons.YesNo);


    if (result1 == DialogResult.Yes)
    {
        Timer tm;
        tm = new Timer();

        int minutes = int.Parse(textBox2.Text);
        tm.Interval = (int)TimeSpan.FromMinutes(minutes).TotalMilliseconds;

        tm.Tick += new EventHandler(button1_Click);

        tm.Enabled = true;

        string pastebuffer = DateTime.Now.ToString();
        pastebuffer = "### Edited on " + pastebuffer + " by " + txtUsername.Text + " ###";
        Clipboard.SetText(pastebuffer);

        tm.Start();

    }

    else if (result1 == DialogResult.No)
    { 

    }

    this.TopMost = false;
}

If I have defined 15 mins in my interval how do i get the countdown to show in a label?


回答1:


You should store end-time in a filed at form level and then in Tick event handler of the timer check the difference between the end-time and now and update a label which you want to show count-down timer:

private DateTime endTime;
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
private void button1_Click(object sender, EventArgs e)
{
    var minutes = 0;
    if (int.TryParse(textBox1.Text, out minutes) && timer.Enabled == false)
    {
        endTime = DateTime.Now.AddMinutes(minutes);
        timer.Interval = 1000;
        timer.Tick -= new EventHandler(timer_Tick);
        timer.Tick += new EventHandler(timer_Tick);
        timer.Start();
        UpdateText();
    }
}
void timer_Tick(object sender, EventArgs e)
{
    UpdateText();
}
void UpdateText()
{
    var diff = endTime.Subtract(DateTime.Now);
    if (diff.TotalSeconds > 0)
        label1.Text = string.Format("{0:D2}:{1:D2}:{2:D2}",
                                   diff.Hours, diff.Minutes, diff.Seconds);
    else
    {
        this.Text = "00:00:00";
        timer.Enabled = false;
    }
}



回答2:


I wouldn't muck about with timers. I'd use Microsoft's Reactive Framework for this. Just NuGet "Rx-Winforms" to get the bits. Then you can do this:

Observable
    .Create<string>(o =>
    {
        var now = DateTimeOffset.Now;
        var end = now.AddMinutes(15.0);
        return
            Observable
                .Interval(TimeSpan.FromSeconds(0.1))
                .TakeUntil(end)
                .Select(x => end.Subtract(DateTimeOffset.Now).ToString(@"mm\:ss"))
                .DistinctUntilChanged()
                .Subscribe(o);
    })
    .ObserveOn(this)
    .Subscribe(x => label1.Text = x);

This will automatically create a countdown timer that will update the text in label1 with the following values:

14:59 
14:58 
14:57 
14:56 
14:55 
14:54 
...
00:02
00:01
00:00

If you want to stop this before the timer runs out the Subscribe method returns an IDisposable that you can just call .Dispose().




回答3:


You should try to count the 15 minutes. For example, if your using a Label (Label1) you should count it with a timer. Just use a timer and count every tick (1000 milliseconds) +1

Timer1 has a +1 (declare a int as 0)
If the label reaches the number of seconds or minutes
(You can modify that with milliseconds), it stops the timer.


来源:https://stackoverflow.com/questions/38420596/display-time-elapsed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!