⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dateedit.cs

📁 C#写成的PB形式的时间控件
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Text.RegularExpressions;
namespace myCombo_src
{
    public class DateEdit : MaskedTextBox
    {
        public DateEdit()
        {
            this.KeyDown += new KeyEventHandler(ClassMask_KeyDown);
            base.InsertKeyMode = InsertKeyMode.Overwrite;
            base.TextMaskFormat = MaskFormat.IncludePromptAndLiterals;
            base.ResetOnSpace = false;
            base.ResetOnPrompt = true;
            base.PromptChar = '0';
            base.TextAlign = HorizontalAlignment.Left;
            base.Mask = "0000-00-00 00:00";
        }
        private MaskType _maskType = MaskType.Long;
        private bool _isLongType = true;
        /// <summary>
        /// true表示当前的向左键是由退格键转换的
        /// </summary>
        private bool _isBack;
        /// <summary>
        /// true表示当前的向右键是由delete键转换的
        /// </summary>
        private bool _isDelete;
        #region Property
        [Browsable(false)]
        public bool IsLongType
        {
            get { return _isLongType; }
            set { _isLongType = value; }
        }
        [Browsable(false)]
        public new InsertKeyMode InsertKeyMode
        {
            get { return base.InsertKeyMode; }
            set
            {
                if (value != InsertKeyMode.Overwrite)
                    throw new NotSupportedException("Not Support the other ");
                base.InsertKeyMode = value;
            }

        }
        [Browsable(false)]
        public new MaskFormat TextMaskFormat
        {
            get { return base.TextMaskFormat; }
            set
            {
                if (value != MaskFormat.IncludePromptAndLiterals)
                    throw new NotSupportedException("Not Support the other ");
                base.TextMaskFormat = value;
            }
        }
        [Browsable(false)]
        public new bool ResetOnSpace
        {
            get { return base.ResetOnSpace; }
            set
            {
                if (value != false)
                    throw new NotSupportedException("Not Support the other");
                base.ResetOnSpace = value;
            }
        }
        [Browsable(false)]
        public new bool ResetOnPrompt
        {
            get { return base.ResetOnPrompt; }
            set
            {
                if (value != true)
                    throw new NotSupportedException("Not Support the other");
                base.ResetOnPrompt = value;
            }
        }
        [Browsable(false)]
        public new char PromptChar
        {
            get { return base.PromptChar; }
            set
            {
                if (value != '0')
                    throw new NotSupportedException("Not Support the other");
                base.PromptChar = value;
            }
        }
        [Browsable(false)]
        public new HorizontalAlignment TextAlign
        {
            get { return base.TextAlign; }
            set
            {
                if (value != HorizontalAlignment.Left)
                    throw new NotSupportedException("Not Support the other");
                base.TextAlign = value;
            }
        }
        [Browsable(false)]
        public new string Mask
        {
            get { return base.Mask; }
            set
            {
                if (value == "0000-00-00 00:00")
                {
                    this._isLongType = true;
                }
                else
                    this._isLongType = false;
                base.Mask = value;
            }
        }
        [Description("Mask格式种类枚举")]
        public MaskType MaskType
        {
            get { return _maskType; }
            set
            {
                if (value == MaskType.Long)
                {
                    base.Mask = "0000-00-00 00:00";
                }
                else
                {
                    base.Mask = "0000-00-00";
                }
                _maskType = value;
            }
        }
        #endregion
        #region Event
        private void ClassMask_KeyDown(object sender, KeyEventArgs e)
        {
            int inputValue;
            int onePlace = 0;
            int correctPlace = this.SelectionStart;
            int selectIndex;
            string text = "";
            inputValue = getIntByCode(e);
            //不是数字键
            if (inputValue == -1)
            {
                text = this.Text;
                if ((e.KeyCode == Keys.Right && _isDelete) || (e.KeyCode == Keys.Left && _isBack))
                {
                    if (e.KeyCode == Keys.Left && _isBack)
                    {
                        if (this.SelectionLength == 0)
                        {
                            selectIndex = this.SelectionStart - 1;
                            if (selectIndex < 0)
                                return;
                            if (checkChar(selectIndex))
                            {
                                text = text.Remove(selectIndex, 1);
                                text = text.Insert(selectIndex, "0");
                                this.Text = text;
                            }
                            if (selectIndex < 0)
                                selectIndex = 0;
                            this.SelectionStart = selectIndex;
                            e.Handled = true;
                            _isBack = false;
                            return;
                        }
                    }
                    else
                    {
                        if (this.SelectionLength == 0)
                        {
                            selectIndex = this.SelectionStart;
                            if (selectIndex > this.Text.Length - 1)
                                return;
                            if (checkChar(selectIndex))
                            {
                                text = text.Remove(selectIndex, 1);
                                text = text.Insert(selectIndex, "0");
                                this.Text = text;
                            }
                            selectIndex++;
                            if (selectIndex > this.Text.Length - 1)
                                selectIndex = this.Text.Length;
                            this.SelectionStart = selectIndex;
                            e.Handled = true;
                            _isDelete = false;
                            return;
                        }
                    }
                    //时间文本框中选定的字符数大于零时
                    if (this.SelectionLength > 0)
                    {
                        selectIndex = this.SelectionStart;
                        this.Text = removeString(this.SelectionStart, this.Text.Length);
                        this.SelectionStart = selectIndex;
                        e.Handled = true;
                        _isBack = false;
                        _isDelete = false;
                    }

                }
                return;
            }
            //如果文本框中选定的字符数大于零
            if (this.SelectionLength > 0)
            {
                this.Text = removeString(this.SelectionStart, this.Text.Length);
            }

            onePlace = getPlace(correctPlace, inputValue);

            if (onePlace != -1 && onePlace != -2)
                correctPlace = onePlace;
            if (onePlace > this.Text.Length - 1)
            {
                correctPlace = this.Text.Length;

            }
            this.SelectionStart = correctPlace;
        }
        #endregion
        #region Method
        /// <summary>
        /// 退格键或del键换成向左键
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {

            if (keyData == Keys.Back)
            {
                if (this.ContainsFocus)
                {
                    SendKeys.Send("{LEFT}");
                    _isBack = true;
                    return true;
                }
            }
            if (keyData == Keys.Delete)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -