在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → C# Winfrom 自定义日期控件

C# Winfrom 自定义日期控件

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.06M
  • 下载次数:138
  • 浏览次数:2484
  • 发布时间:2014-09-24
  • 实例类别:C#语言基础
  • 发 布 人:Yankeyli
  • 文件格式:.rar
  • 所需积分:2
 相关标签: C#

实例介绍

【实例简介】
【实例截图】 

【核心代码】

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace test
{
    public partial class UCL_Datetime : UserControl
    {
        /// <summary>
        /// 自定义日期控件
        /// </summary>
        public UCL_Datetime()
        {
            //属性的构造是先于构造函数的
            InitializeComponent();
            this.ControlAdded  = new ControlEventHandler(UCL_Datetime_ControlAdded);
        }

        void UCL_Datetime_ControlAdded(object sender, ControlEventArgs e)
        {
            textbox_DateTime.Name = this.Name;
        }

        MonthCalendar g_month = new MonthCalendar();

        #region 属性值设置
        bool check_datetime = true;
        bool textbox_Enabled = true;
        private string datetime_formate = "yyyy-MM-dd";
        //string textbox_text;
        //string textbox_name;

        /// <summary>
        /// 该控件是否已启用
        /// </summary>
        [DefaultValue(""), Description("该控件是否已启用"), Category("Appearance"), Browsable(true)]
        public bool a_Enabled
        {
            get
            {
                return textbox_Enabled;
            }
            set
            {
                textbox_Enabled = value;
                textbox_DateTime.Enabled = textbox_Enabled;
                customButton1.Enabled = textbox_Enabled;
            }
        }
        /// <summary>
        /// 日期控件的值
        /// </summary>
        [DefaultValue(""), Description("日期控件的值"), Category("Appearance"), Browsable(true)]
        public string a_Text
        {
            get
            {
                //return textbox_text;
                return textbox_DateTime.Text;
            }
            set
            {
                //textbox_text = value;
                //textbox_DateTime.Text = textbox_text;
                textbox_DateTime.Text = value;
            }
        }
        /// <summary>
        /// 光标离开控件时,是否需要判断日期输入的正确性 
        /// </summary>
        [DefaultValue("True"), Description("光标离开控件时,是否需要判断日期输入的正确性"), Category("Appearance"), Browsable(true)]
        public bool a_Check
        {
            get
            {
                return check_datetime;
            }
            set
            {
                check_datetime = value;
            }
        }

        /// <summary>
        /// 日期控件的自定义格式
        /// </summary>
        [DefaultValue(""), Description("日期控件的自定义格式"), Category("Appearance"), Browsable(true)]
        public string a_Formate
        {
            get
            {
                return datetime_formate;
            }
            set
            {
                datetime_formate = value;
            }
        }

        /// <summary>
        /// 日期控件的名称,与本控件的名称相同,且与对应表的列名相同,隐藏的属性
        /// </summary>
        [DefaultValue(""), Description("日期控件的名称,与本控件的名称相同,且与对应表的列名相同"), Category("Appearance"), Browsable(false)]
        public string a_textbox_Name
        {
            get
            {
                return this.Name;
            }
            set
            {
                textbox_DateTime.Name = value;
            }
        }
        #endregion 属性值设置

        /// <summary>
        /// 日期值改变时的委托
        /// </summary>
        public delegate void Text_Changed(object sender, EventArgs e);
        /// <summary>
        /// 日期值改变时的事件
        /// </summary>
        public event Text_Changed TextChanged_event;


        private void UCL_Datetime_Load(object sender, EventArgs e)
        {
            customButton1.Dock = DockStyle.Right;
            customButton1.FlatStyle = FlatStyle.Popup;
            customButton1.Width = 21;
            customButton1.BackColor = Color.Gainsboro;
            customButton1.BackgroundImageLayout = ImageLayout.Zoom;
            customButton1.DisplayFocusCues = false;

            toolTip1.SetToolTip(textbox_DateTime, this.Name);

            customButton1.Click  = new EventHandler(customButton1_Click);

            g_month.DateSelected  = new DateRangeEventHandler(month_DateSelected);
            g_month.Leave  = new EventHandler(month_Leave);
            //如果使用textbox_DateTime.Validating事件,如果输入的值是错误的,再开窗时会显示错误信息,所以使用this.Validating
            //this.Validating  = new CancelEventHandler(textbox_DateTime_Validating);
            textbox_DateTime.Validating  = new CancelEventHandler(textbox_DateTime_Validating);

            textbox_DateTime.TextChanged  = new EventHandler(textbox_DateTime_TextChanged);
        }

        void textbox_DateTime_TextChanged(object sender, EventArgs e)
        {
            TextChanged_event(sender, e);
        }


        //
        void textbox_DateTime_Validating(object sender, CancelEventArgs e)
        {
            //如果控件不可交互,则不用验证
            if (textbox_DateTime.Enabled == false)
            {
                return;
            }
            if (a_Check == true)
            {
                //这里还有一个错误,如果输入内容不对,点击那个BUTTON显示日期时,还是显示不到
                if (g_month.Focused == false && string.IsNullOrEmpty(textbox_DateTime.Text) == false)
                {
                    DateTime dt;
                    if (!DateTime.TryParse(textbox_DateTime.Text, out dt))
                    {
                        //PUB.pub_s_Message.fun_message("ucl-0003", 1, 5); //日期格式不对
                        e.Cancel = true;
                        textbox_DateTime.SelectAll();
                    }
                }
            }
        }

        //光标离开  month 时,移除 month
        void month_Leave(object sender, EventArgs e)
        {
            Control l_control = g_month;
            while (l_control.Parent != null)
            {
                l_control = l_control.Parent;
                if (l_control.Controls.Contains(g_month))
                {
                    l_control.Controls.Remove(g_month);
                    return;
                }
            }
        }

        //在 month 中选择日期后,移除month并给textbox赋值
        void month_DateSelected(object sender, DateRangeEventArgs e)
        {
            textbox_DateTime.Text = e.Start.ToString(datetime_formate);

            textbox_DateTime.Focus();

            Control l_control = g_month;
            while (l_control.Parent != null)
            {
                l_control = l_control.Parent;
                if (l_control.Controls.Contains(g_month))
                {
                    l_control.Controls.Remove(g_month);
                    return;
                }
            }
        }

        //弹出日期控件
        void customButton1_Click(object sender, EventArgs e)
        {
            if (textbox_DateTime.Enabled == false)
            {
                return;
            }

            if (this.Parent.Parent != null)
            {
                if (this.Parent.Parent.GetType().ToString() == "System.Windows.Forms.DataGridView")
                {
                    DataGridView l_dtgv = (DataGridView)this.Parent.Parent;

                    int l_x = l_dtgv.Location.X   this.Parent.Location.X;
                    int l_y = l_dtgv.Location.Y   this.Parent.Location.Y   textbox_DateTime.Height;

                    //当行在右侧时,父控件右方空间不足以显示日期控件,则在父控件左方显示
                    if (l_dtgv.Width - this.Parent.Location.X < g_month.Width)
                    {
                        l_x = l_dtgv.Location.X   this.Parent.Location.X - g_month.Width;
                    }

                    //当行在底部时,父控件的下方空间不足并且上方空间充足的情况下在父控件上方显示日期控件,则在BUTTON的上方显示
                    if (l_dtgv.Height - this.Parent.Location.Y < g_month.Height && this.Parent.Location.Y > g_month.Height)
                    {
                        l_y = l_dtgv.Location.Y   this.Parent.Location.Y - g_month.Height;
                    }

                    g_month.Location = new Point(l_x,l_y); 
                    l_dtgv.Parent.Controls.Add(g_month);
                }
                else
                {
                    g_month.Location = new Point(this.Parent.Location.X this.Location.X, this.Parent.Location.Y this.Location.Y   textbox_DateTime.Height);
                    this.Parent.Parent.Controls.Add(g_month);
                }
            }
            else
            {
                g_month.Location = new Point(this.Location.X, this.Location.Y   textbox_DateTime.Height);
                this.Parent.Controls.Add(g_month);
            }

            g_month.BringToFront();
            g_month.MaxSelectionCount = 1;
            g_month.Focus();

            if (string.IsNullOrEmpty(textbox_DateTime.Text) == false)
            {
                try
                {
                    g_month.SelectionStart = DateTime.Parse(textbox_DateTime.Text);
                }
                catch
                {
                    g_month.SelectionStart = DateTime.Now;
                }
            }
            else
            {
                g_month.SelectionStart = DateTime.Now;
            }
            g_month.SelectionEnd = g_month.SelectionStart;
        }
    }

    /// <summary>
    /// 为了不显示BUTTON的聚焦框,自定义一个BUTTON,因为ShowFocusCues属性是受保护的,所以需要重写这个BUTTON的ShowFocusCues
    /// </summary>
    class CustomButton : System.Windows.Forms.Button
    {
        private bool _DisplayFocusCues = true;
        protected override bool ShowFocusCues
        {
            get
            {
                return _DisplayFocusCues;
            }
        }
        /// <summary>
        /// 是否显示聚焦框,重写的属性 
        /// </summary>
        [DefaultValue("True"), Description("是否显示聚焦框,重写的属性"), Category("Appearance")]
        public bool DisplayFocusCues
        {
            get
            {
                return _DisplayFocusCues;
            }
            set
            {
                _DisplayFocusCues = value;
            }
        }
    }








    #region 以下为自定义列类型代码段

    public class UCL_Datetime_Column : DataGridViewColumn
    {
        public UCL_Datetime_Column()
            : base(new UCL_Datetime_Cell())
        {
        }

        public override DataGridViewCell CellTemplate
        {
            get
            {
                return base.CellTemplate;
            }
            set
            {
                // Ensure that the cell used for the template is a UCL_Datetime_Cell.
                if (value != null && !value.GetType().IsAssignableFrom(typeof(UCL_Datetime_Cell)))
                {
                    throw new InvalidCastException("Must be a UCL_Datetime_Cell");
                }
                base.CellTemplate = value;
            }
        }
        public override object Clone()
        {
            UCL_Datetime_Column col = (UCL_Datetime_Column)base.Clone();

            return col;
        }

    }

    public class UCL_Datetime_Cell : DataGridViewTextBoxCell
    {

        public UCL_Datetime_Cell()
            : base()
        {
            // Use the short date format.
            this.Style.Format = "d";
        }

        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            // Set the value of the editing control to the current cell value.
            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
            UCL_Datetime_EditingControl ctl = DataGridView.EditingControl as UCL_Datetime_EditingControl;
            if (this.Value != null)
            {
                ctl.a_Text = this.Value.ToString();
            }
            else
            {
                ctl.a_Text = DateTime.Now.ToShortDateString();
            }
        }

        public override Type EditType
        {
            get
            {
                // Return the type of the editing contol that UCL_Datetime_Cell uses.
                return typeof(UCL_Datetime_EditingControl);
            }
        }

        //public override Type ValueType
        //{
        //    get
        //    {
        //        // Return the type of the value that UCL_Datetime_Cell contains.
        //        return typeof(DateTime);
        //    }
        //}

        public override object DefaultNewRowValue
        {
            get
            {
                // Use the current date and time as the default value.
                return DateTime.Now;
            }
        }
    }

    class UCL_Datetime_EditingControl : UCL_Datetime, IDataGridViewEditingControl
    {
        DataGridView dataGridView;
        private bool valueChanged = false;
        int rowIndex;

        public UCL_Datetime_EditingControl()
        {
            this.a_Formate = "yyyy-MM-dd";
            this.TextChanged_event  = new Text_Changed(UCL_Datetime_EditingControl_TextChanged_event);
        }

        void UCL_Datetime_EditingControl_TextChanged_event(object sender, EventArgs e)
        {
            valueChanged = true;
            this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
            //this.EditingControlFormattedValue = this.a_Text;
            //base.OnDateChanged(sender, e);
            //base.OnTextChanged_event(sender, e);
        }

        // Implements the IDataGridViewEditingControl.EditingControlFormattedValue 
        // property.
        public object EditingControlFormattedValue
        {
            get
            {
                return this.a_Text;
            }
            set
            {
                String newValue = value as String;
                if (newValue != null)
                {
                    this.a_Text = DateTime.Parse(newValue).ToShortDateString();
                }
            }
        }

        // Implements the 
        // IDataGridViewEditingControl.GetEditingControlFormattedValue method.
        public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
        {
            return EditingControlFormattedValue;
        }

        // Implements the 
        // IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
        public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
        {
            this.Font = dataGridViewCellStyle.Font;
            this.ForeColor = dataGridViewCellStyle.ForeColor;
            this.BackColor = dataGridViewCellStyle.BackColor;
        }

        // Implements the IDataGridViewEditingControl.EditingControlRowIndex 
        // property.
        public int EditingControlRowIndex
        {
            get
            {
                return rowIndex;
            }
            set
            {
                rowIndex = value;
            }
        }

        // Implements the IDataGridViewEditingControl.EditingControlWantsInputKey 
        // method.
        public bool EditingControlWantsInputKey(Keys key, bool dataGridViewWantsInputKey)
        {
            // Let the DateTimePicker handle the keys listed.
            switch (key & Keys.KeyCode)
            {
                case Keys.Left:
                case Keys.Up:
                case Keys.Down:
                case Keys.Right:
                case Keys.Home:
                case Keys.End:
                case Keys.PageDown:
                case Keys.PageUp:
                    return true;
                default:
                    return false;
            }
        }

        // Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit 
        // method.
        public void PrepareEditingControlForEdit(bool selectAll)
        {
            // No preparation needs to be done.
        }

        // Implements the IDataGridViewEditingControl
        // .RepositionEditingControlOnValueChange property.
        public bool RepositionEditingControlOnValueChange
        {
            get
            {
                return false;
            }
        }

        // Implements the IDataGridViewEditingControl
        // .EditingControlDataGridView property.
        public DataGridView EditingControlDataGridView
        {
            get
            {
                return dataGridView;
            }
            set
            {
                dataGridView = value;
            }
        }

        // Implements the IDataGridViewEditingControl
        // .EditingControlValueChanged property.
        public bool EditingControlValueChanged
        {
            get
            {
                return valueChanged;
            }
            set
            {
                valueChanged = value;
            }
        }

        // Implements the IDataGridViewEditingControl
        // .EditingPanelCursor property.
        public Cursor EditingPanelCursor
        {
            get
            {
                return base.Cursor;
            }
        }

        //protected override void OnTextChanged_event(object sender, EventArgs eventargs)
        //{
        //    // Notify the DataGridView that the contents of the cell
        //    // have changed.
        //    valueChanged = true;
        //    this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
        //    //this.OnDateChanged(sender,eventargs);
        //    //this.OnTextChanged_event(sender, eventargs);
        //    //base.OnTextChanged_event(sender, eventargs);
        //    this.OnTextChanged_event(sender, eventargs);

        //}

    #endregion 
    }
}

标签: C#

实例下载地址

C# Winfrom 自定义日期控件

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有2条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警