如何只在一个标签中显示三个文本框的时间?

时间:2017-05-29 05:21:05

标签: c# wpf

private void timer_click1(object sender, RoutedEventArgs e)
{
    seconds = seconds - 1;
    if (seconds == -1)
    {
        minutes = minutes - 1;
        seconds = 59;
    }
    if (minutes == -1)
    {
        hours = hours - 1;
        minutes = 59;
    }
    if (hours == 0 && minutes == 0 && seconds == 0)
    {
        timer.Stop();
    }
    string hhr = Convert.ToString(hours);
    string minns = Convert.ToString(minutes);
    string seccs = Convert.ToString(seconds);
}

我正在尝试创建一个倒数计时器。我坚持使用hhr,minns和seccs,因为我必须在一个标签中显示倒计时而不是更多。我如何将这三个文本作为hrr:minns:seccs?

传递

1 个答案:

答案 0 :(得分:1)

可能有更好的方法来获取剩余时间,但以下是使用string.Format方法在单个string中显示所有三个组件的方法:

private void timer_click1(object sender, RoutedEventArgs e)
{
    seconds = seconds - 1;
    if (seconds == -1)
    {
        minutes = minutes - 1;
        seconds = 59;
    }
    if (minutes == -1)
    {
        hours = hours - 1;
        minutes = 59;
    }
    if (hours == 0 && minutes == 0 && seconds == 0)
    {
        timer.Stop();
    }
    string hhr = Convert.ToString(hours);
    string minns = Convert.ToString(minutes);
    string seccs = Convert.ToString(seconds);

    string s = string.Format("{0}:{1}:{2}", hhr, minns, seccs);
    lbl.Text = s;
}

在C#6 +它甚至是eaiser:

string s = $"{hhr}:{minns}:{seccs}";