I am using the following code to display the elapsed time of a task in the status bar in my application.
public void DisplayDuration(TimeSpan duration)
{
string formattedDuration;
if (duration.TotalMilliseconds < 2000)
formattedDuration = string.Format("{0} ms", duration.TotalMilliseconds);
else if (duration.TotalSeconds < 60)
formattedDuration = string.Format("{0} sec", duration.TotalSeconds);
else
formattedDuration = string.Format("{0} min", duration.TotalMinutes);
this.TimingLabel.Text = formattedDuration;
}
this.TimingLabel is a label in the statusStrip control in the footer of the winform.
But I get completely different results on Windows XP vs Windows 7
Windows XP:
Windows 7
Why is the units appearing before the time in Windows 7?
I have checked Regional Settings both machines are set to US with same Date Time formatting. Make quite quite sure it is the same code running on both machines. This is very odd behavior in some very simple code.
As a follow up: I made the following change to my code but still have the same problem:
formattedDuration = string.Format("{0} ms", duration.TotalMilliseconds.ToString());
I think the most likely probably here is a layout issue and not String.Format
. Regional settings shouldn't be a factor here because you're not asking the TimeSpan
to format its value. Instead you're asking String
to format a string "ms" followed by a number. It would be simply a bug if it inverted them in the output.
What's much more likely is that the number is being clipped via a bug in the layout constraints of the container. If I look very hard at the screen shot there does appear to be a divider immediately to the left of the ms
string.
Try giving everything a fixed width which is fairly large and see if the correct display comes back.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With