可设置空值(Nullable)的DateTimePicker

这篇博客介绍了如何在WinForm应用中扩展DateTimePicker控件,使其支持空值(Nullable)。作者针对原DateTimePicker无法设置Null的问题进行了改造,并且欢迎读者在发现任何问题时进行反馈。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于WinForm自带的DateTimePicker不能设置空值(Null),所以我基于原来的DateTimePicker做了扩展。

若有bug,可反馈,我再修改。


namespace WinFormTest
{
    public class DateTimePicker2 : DateTimePicker
    {
        const string NullableFormat = " ";
        bool isSelfSetting;
        string originalCustomFormat;
        bool originalCustomFormatInitialized;
        DateTimePickerFormat? originalFormat;

        bool IsNullableState
        {
            get { return Format == DateTimePickerFormat.Custom && CustomFormat == NullableFormat; }
        }

        void SetNullable(bool nullable)
        {
            if (!this.originalFormat.HasValue)
            {
                this.originalFormat = Format;
            }
            if (!this.originalCustomFormatInitialized)
            {
                this.originalCustomFormat = CustomFormat;
                this.originalCustomFormatInitialized = true;
            }

            if (!Nullable) return;

            this.isSelfSetting = true;
            Format = nullable ? DateTimePickerFormat.Custom : this.originalFormat.Value;
            CustomFormat = nullable ? NullableFormat : this.originalCustomFormat;
            this.isSelfSetting = false;
        }

        #region Properties

        public bool Nullable { get; set; }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new DateTime? Value
        {
            get { return IsNullableState ? (DateTime?)null : base.Value; }
            set
            {
                if (value.HasValue)
                    base.Value = value.Value;
                SetNullable(!value.HasValue);
            }
        }

        public new DateTimePickerFormat Format
        {
            get { return base.Format; }
            set
            {
                if (!this.isSelfSetting)
                    this.originalFormat = value;
                base.Format = value;
            }
        }

        public new string CustomFormat
        {
            get { return base.CustomFormat; }
            set
            {
                if (!this.isSelfSetting)
                {
                    this.originalCustomFormat = value;
                    this.originalCustomFormatInitialized = true;
                }
                base.CustomFormat = value;
            }
        }

        #endregion

        #region Events  

        protected override void OnGotFocus(EventArgs e)
        {
            if (IsNullableState)
                SetNullable(false);
            base.OnGotFocus(e);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (IsNullableState)
                SetNullable(false);
            base.OnMouseDown(e);
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            SetNullable(e.KeyCode == Keys.Back || e.KeyCode == Keys.Delete);
            base.OnKeyDown(e);
        }

        #endregion
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值