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}";
}
}
>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.