我让用户使用两个NumericUpDown
控件选择运行计划任务的日期/时间。
我希望用前导 0 填充一位数值,以便显示09:00
而不是9:0
.
我让用户使用两个NumericUpDown
控件选择运行计划任务的日期/时间。
我希望用前导 0 填充一位数值,以便显示09:00
而不是9:0
.
最终的解决方案是使用DateTimePicker
with ShowUpDown
set toTrue
和Format
set to Time
or Custom
。在后一种情况下,您将使用hh:mm
orHH:mm
作为自定义格式。
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);
}
}
}
今天早上我必须这样做,并为我的 Windows 窗体应用程序想出了一个自定义的数字 Up Down。您应该能够轻松地将其更改为 VB.NET。
这对于 NumericUpDown 控件是不可能的。
我有个聪明的主意~为什么不放一个文本框覆盖numericupdown控件的文本框部分(只会显示numericupdown的滚动)?
将您的文本框设置为“00”作为初始值,然后禁用它,这样用户就无法控制您的文本框。
然后输入这些代码:
Private Sub numericupdown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ndFrom.ValueChanged
If numericupdown1.Value < 10 Then
textbox1.Text = "0" & numericupdown1.Value
Else
textbox1.Text = numericupdown1.Value
End If
End Sub
class MyNumericUpDown : System.Windows.Forms.NumericUpDown
{
public override string Text
{
get
{
return base.Text;
}
set
{
if (value.Length < 2)
value = "0" + value;
base.Text = value;
}
}
}