Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to set ToString("00") for hours/minutes/seconds when changed on button click

I have a WPF App where I have a Timer, I did everything accept how to when the numbers of hours/minutes/seconds are changing on button click, they should stay in this format: 01, 02, 03 etc. and by me it’s 1, 2, 3 etc.

here is my code for hours Up and Down click:

private void btnHoursPlus_Click(object sender, RoutedEventArgs e)
        {

            int number = Convert.ToInt32(tbHours.Text);
            if (number < 24)
            {
                tbHours.Text = $"{number + 1}";
            }
        }

-------------------------------------------------------------------------------


private void btnHoursMinus_Click(object sender, RoutedEventArgs e)
        {
            int number = Convert.ToInt32(tbHours.Text);
            if (number > 0)
            {
                tbHours.Text = $"{number - 1}";
            }
        }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can specify the number of digits when converting to a string.

tbHours.Text = $"{number - 1:D2}";
// result 01, 02, 03 ...

More information can be found in the Decimal entry on the link.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading