[英]MessageBox.Show(TimeSpan)
i would like to show a TimeSpan
in a MessageBox
but am getting an error: 我想在MessageBox
显示TimeSpan
,但出现错误:
DateTime date1 = new DateTime(byear, bmonth, bday, 0, 0, 0);
DateTime datenow = DateTime.Now;
TimeSpan age = datenow - date1;
MessageBox.Show(ToString(age));
Error 1 No overload for method 'ToString' takes '1' arguments
how do i output a messagebox with TimeSpan
? 如何使用TimeSpan
输出消息框?
MessageBox.Show(age.ToString());
Though you might not like the result. 虽然您可能不喜欢结果。 If you want a specific format you have to implement it yourself. 如果需要特定的格式,则必须自己实现。
That's not going to look great, TimeSpan is missing a decent ToString() override on .NET 3.5 and earlier. 这看起来并不好,TimeSpan在.NET 3.5及更早版本上缺少不错的ToString()覆盖。 Work around that by using the DateTime.ToString() method: 通过使用DateTime.ToString()方法来解决此问题:
string txt = new DateTime(Math.Abs(age.Ticks)).ToString("h:mm:ss");
if (age.Ticks < 0) txt = "-" + txt;
MessageBox.Show(txt);
你必须做age.ToString()
或者您可以执行Convert.ToString(age)
来保持您现在的格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.