c# - 在 numericupdown 控件中显示前导零 (C#)

标签 c# visual-studio-2008 controls

我在 C# Windows 窗体上有一个 numericupdown 控件,如果它小于 10,我有兴趣在它的值中添加一个前导零。(它供用户输入时间的分钟值。)

我还不太熟悉 C# 中的覆盖/继承,但看起来我可能必须这样做。

看起来像this post EggheadCafe 上有我需要的答案。是否像创建一个新类然后创建该新类的控件一样简单?

public class TestNum : NumericUpDown
{
protected override void ValidateEditText()
{
if (base.UserEdit)
{
base.ValidateEditText();
}
}

protected override void UpdateEditText()
{
Text = Convert.ToInt32(base.Value).ToString("00");
}
}

当我尝试这样做时,我不确定如何创建利用此类的新控件。我正在使用 Visual Studio 2008。Windows 窗体仍然很新。感谢您的任何建议。

编辑

我能够通过编辑 Designer 创建的代码来完成这项工作,这样新控件就不再属于原始类,而是属于新类。所以在添加上面的类之后,我做了以下操作(这些更改在两个不同的位置,但我只显示重要的行):

更改:

this.numTest = new System.Windows.Forms.NumericUpDown();
private System.Windows.Forms.NumericUpDown numTest;

收件人:

this.numTest = new SampleForm.TestNum();
private TestNum numTest;

最佳答案

为什么不直接使用 DateTimePicker 控件呢?将其 ShowNumericUpDown 属性设置为 true,将其 Format 属性设置为 Custom,将 CustomFormat 属性设置为 hh:mm:ss。

也许这对你有用。

关于c# - 在 numericupdown 控件中显示前导零 (C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1358425/

相关文章:

c# - 申请.退出

C# 列出目录中的文件问题

apache-flex - 柔性 4 : Controls in spark Panel still show when outside of panel size

C# WebBrowser 控件 - 脚本错误,解决方法?

c# - MVVM跨安卓: value with binding not updated

c# - 将水印图像添​​加到图像

c# - Twilio c# helper,如何使用此方法签名? void AddOutgoingCallerId (phoneNumber, friendlyName, callDelay, callback)

c# - 使用 Visual Studio 2008 错误创建输出文件夹

c# - 对于 EF linq 查询,First(c=>cond) 是否等同于 Where(c=>cond).First()?

html - 防止HTML5标准视频控件淡出?