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?
传递答案 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}";