Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.format() value in statusstrip label displayed differently on Win 7 vs Win XP

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: elapsed Time formatted XP

Windows 7 elapsed Time formatted 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()); 
like image 921
Gary Kindel Avatar asked Jan 06 '12 16:01

Gary Kindel


1 Answers

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.

like image 193
JaredPar Avatar answered Oct 19 '22 05:10

JaredPar