0

Hello Everybody !!!

I have the problem about my Digital Clock, You could see this on my attachment.

It works perfectly, but one problem that when the second == 10, the label name second lblSed appear "010", when it's up to 11, it appear normally. I dont know what wrong with that.

Thanks for helping !!!

My timer Even code:

timer1.Interval = 1000;
 if (sed >= 10 || min >= 10 || hour >= 10)
{
            sed++;
            lblSed.Text = sed.ToString();
            if (sed == 60)
            {
                sed = 0;
                min++;
                lblMin.Text = min.ToString();
            }
            if (min == 60)
            {
                min = 0;
                hour++;
                lblHour.Text = hour.ToString();
            }
        }
        else
        {
            if (sed < 10 || min < 10 || hour < 10)
            {
                sed++;
                lblSed.Text = "0" + sed.ToString();
                if (sed == 60)
                {
                    sed = 0;
                    min++;
                    lblMin.Text = "0" + min.ToString();
                }
                if (min == 60)
                {
                    min = 0;
                    hour++;
                    lblHour.Text = "0" + hour.ToString();
                }
            }
}

Digital_Clock.zip

Edited by pyTony: fixed formatting

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Momerath
0

In line 21 you ask the question "is any of these values less than 10?" If it is then you prepend 0 *to all of them*. You'd be better off getting rid of the check entirely and using String.Format:

...
sed++;
if (sed == 60) {
    sed = 0;
    min++;
    if (min == 60) {
        min = 0;
        hour++;
    }
}
lblSed.Text = String.Format("{0:00}", sed);
lblMin.Text = String.Format("{0:00}", min);
lblHour.Text = String.Format("{0:00}", hour);

...

What the format {0:00} mean is take the first parameter supplied (remember, C# counts from zero so the first parameter is indicated by 0) and apply the format "00" to it, which means at least two digits so put a zero in front if it's only 1 digit.

Edited by Momerath: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.