Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display `00` instead of `0` in a NumericUpDown control

I'm letting users select a date/time for a scheduled task to run, using two NumericUpDowncontrols.

I'd like one-digit values to be padded with a leading 0, so as to display 09:00 instead of 9:0.

like image 404
Clément Avatar asked Aug 17 '10 11:08

Clément


2 Answers

The definitive solution is to use a DateTimePickerwith ShowUpDown set to True and Format set to Time or Custom. In the latter case, you'd use hh:mm or HH:mm as a custom format.

like image 190
Clément Avatar answered Oct 06 '22 01:10

Clément


class CustomNumericUpDown:System.Windows.Forms.NumericUpDown
{
    protected override void OnTextBoxTextChanged(object source, EventArgs e)
    {
        TextBox tb = source as TextBox;
        int val = 0;
        if (int.TryParse(tb.Text,out val))
        {
            if (val < 10)
            {
                tb.Text = "0" + val.ToString();
            }
        }
        else
        {
            base.OnTextBoxTextChanged(source, e);
        }
    }
}

I had to do this this morning and came up with a Customised Numeric Up Down for my Windows Forms application. You should be able to change this easily enough to VB.NET.

like image 29
LeeSalter Avatar answered Oct 06 '22 01:10

LeeSalter