I'm letting users select a date/time for a scheduled task to run, using two NumericUpDown
controls.
I'd like one-digit values to be padded with a leading 0, so as to display 09:00
instead of 9:0
.
The definitive solution is to use a DateTimePicker
with 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With