C# Winform 可空DateTimePicker V1.2

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;

namespace WindowsFormsApplication1.Common
{
    /// <summary>
    /// 描述:可空值的DateTimePicker控件 v1.2
    /// 作者:李伟波
    /// 创建日期:2010-01-16
    /// 修改日期:2012-07-17 修改赋值后不显示时间的BUG、删除时间后再打开面板未选择时间也赋值的问题。
    /// </summary>
    public class DateTimePickerX : DateTimePicker
    {
        public DateTimePickerX()
            : base()
        {
            SetNullText();
        }

        /// <summary>
        /// 值是否已改变
        /// </summary>
        private bool IsValueChanged = false;
        /// <summary>
        /// 原格式字符串
        /// </summary>
        private string _formatString = "yyyy年MM月dd日";

        /// <summary>
        /// 原格式
        /// </summary>
        private DateTimePickerFormat _originalFormat = DateTimePickerFormat.Custom;

        /// <summary>
        /// 可空日期
        /// </summary>
        private DateTime? valueX = null;

        /// <summary>
        /// 日期(值改变前为null)
        /// </summary>
        [Browsable(true), Category("扩展属性"), Description("可空日期(值改变前为null)")]
        public DateTime? ValueX
        {
            get { return valueX; }
            set
            {
                if (value == null)
                {
                    valueX = value;
                    SetNullText();
                }
                else
                {
                    this.CustomFormat = FormatString;
                    this.Value = value.Value;
                    valueX = value;
                }
            }
        }
        /// <summary>
        /// 格式字符串
        /// </summary>
        [Browsable(true), Category("扩展属性"), Description("格式字符串")]
        public string FormatString
        {
            get { return _formatString; }
            set { _formatString = value; }
        }
        /// <summary>
        /// 设置格式
        /// </summary>
        [Browsable(true), Category("扩展属性"), Description("设置格式")]
        public DateTimePickerFormat OriginalFormat
        {
            get { return _originalFormat; }
            set { _originalFormat = value; }
        }
        /// <summary>
        /// 开始时间(该日期的00:00:00)
        /// </summary>
        [Browsable(true), Category("扩展属性"), Description("开始时间(为当前选中日期的00:00:00)")]
        public DateTime? BeginTime
        {
            get
            {
                if (ValueX.HasValue)
                    return DateTime.Parse(ValueX.Value.ToString("yyyy-MM-dd 00:00:00"));
                else
                    return null;
            }
        }
        /// <summary>
        /// 结束时间(该日期的23:59:59)
        /// </summary>
        [Browsable(true), Category("扩展属性"), Description("结束时间(为当前选中日期的23:59:59)")]
        public DateTime? EndTime
        {
            get
            {
                if (ValueX.HasValue)
                {
                    return DateTime.Parse(ValueX.Value.ToString("yyyy-MM-dd 23:59:59"));
                }
                else
                    return null;
            }
        }

        /// <summary>
        /// 设置空字符显示
        /// </summary>
        private void SetNullText()
        {
            this.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
            this.CustomFormat = " ";
        }

        protected override void OnCloseUp(EventArgs eventargs)
        {
            if (IsValueChanged)
            {
                this.Format = _originalFormat;
                this.CustomFormat = _formatString;
                valueX = this.Value;
                IsValueChanged = false;
            }
            base.OnCloseUp(eventargs);
        }

        protected override void OnValueChanged(EventArgs eventargs)
        {
            this.IsValueChanged = true;
            base.OnValueChanged(eventargs);
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            //删除时间
            if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Delete)
            {
                this.ValueX = null;
                this.CustomFormat = " ";
            }
            base.OnKeyDown(e);
        }

    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值