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

📄 mydatetimecb.cs

📁 C#写成的PB形式的时间控件
💻 CS
字号:
#region Using directive

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

#endregion

namespace myComboBox_src
{
    public partial class myDateTimeCB : ComboBox
    {
        #region Class Data
        private const UInt32 WM_LBUTTONDOWN = 0x201;
        private const UInt32 WM_LBUTTONDBLCLK = 0x203;
        private const UInt32 WM_KEYF4 = 0x134;
        private const UInt32 WM_CTLCOLORLISTBOX = 0x0134;
        ToolStripMonthCalendar myTSMonthCalendar;
        ToolStripDropDown tsDD;
        #endregion

        public myDateTimeCB()
        {
            InitializeComponent();
            myTSMonthCalendar = new ToolStripMonthCalendar();
            tsDD = new ToolStripDropDown();

            // instantiere evenimente
            this.myTSMonthCalendar.MonthCalendarControl.DateChanged += new System.Windows.Forms.DateRangeEventHandler(this.myTSMonthCalendar_DateChanged);
            this.myTSMonthCalendar.MonthCalendarControl.KeyDown += new System.Windows.Forms.KeyEventHandler(this.myTSMonthCalendar_KeyDown);
        }

        #region Control's Methods
        private void myTSMonthCalendar_DateChanged(object sender, DateRangeEventArgs e)
        {
            this.Text = e.End.ToShortDateString();
        }
        private void myTSMonthCalendar_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                this.tsDD.Close();

        }
        #endregion

        #region Methods
        private Point CalculatePoz()
        {
            Point point = new Point(0, this.Height);

            if ((this.PointToScreen(new Point(0, 0)).Y + this.Height + this.myTSMonthCalendar.Height) > Screen.PrimaryScreen.WorkingArea.Height)
            {
                point.Y = -this.myTSMonthCalendar.Height - 7;
            }

            return point;
        }
        #endregion

        protected override void WndProc(ref Message m)
        {
            #region WM_KEYF4
            if (m.Msg == WM_KEYF4)
            {
                this.Focus();
                this.tsDD.Refresh();
                if (!this.tsDD.Visible)
                {
                    try
                    {
                        if (this.Text != "")
                            this.myTSMonthCalendar.MonthCalendarControl.SetDate(Convert.ToDateTime(this.Text));
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Data nu este in formatul corect!");
                    }

                    tsDD.Items.Add(this.myTSMonthCalendar);
                    tsDD.Show(this, this.CalculatePoz());
                }
                return;
            }
            #endregion

            #region WM_LBUTTONDBLCLK
            if (m.Msg == WM_LBUTTONDBLCLK || m.Msg == WM_LBUTTONDOWN)
            {
                if (!this.tsDD.Visible)
                {
                    try
                    {
                        if (this.Text != "")
                            this.myTSMonthCalendar.MonthCalendarControl.SetDate(Convert.ToDateTime(this.Text));
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Data nu este in formatul corect!");
                    }

                    tsDD.Items.Add(this.myTSMonthCalendar);
                    tsDD.Show(this, this.CalculatePoz());
                }
                return;
            }
            #endregion

            base.WndProc(ref m);
        }
    }

    //Declare a class that inherits from ToolStripControlHost.
    public class ToolStripMonthCalendar : ToolStripControlHost
    {
        #region Class Data
        #endregion

        // Call the base constructor passing in a MonthCalendar instance.
        public ToolStripMonthCalendar() : base(new MonthCalendar()) { }

        public MonthCalendar MonthCalendarControl
        {
            get
            {
                return Control as MonthCalendar;
            }
        }

        // Expose the MonthCalendar.FirstDayOfWeek as a property.
        public Day FirstDayOfWeek
        {
            get
            {
                return MonthCalendarControl.FirstDayOfWeek;
            }
            set { value = MonthCalendarControl.FirstDayOfWeek; }
        }

        // Expose the AddBoldedDate method.
        public void AddBoldedDate(DateTime dateToBold)
        {
            MonthCalendarControl.AddBoldedDate(dateToBold);
        }

        // Subscribe and unsubscribe the control events you wish to expose.
        protected override void OnSubscribeControlEvents(Control c)
        {
            // Call the base so the base events are connected.
            base.OnSubscribeControlEvents(c);

            // Cast the control to a MonthCalendar control.
            MonthCalendar monthCalendarControl = (MonthCalendar)c;

            // Add the event.
            monthCalendarControl.DateChanged +=
                new DateRangeEventHandler(OnDateChanged);
        }

        protected override void OnUnsubscribeControlEvents(Control c)
        {
            // Call the base method so the basic events are unsubscribed.
            base.OnUnsubscribeControlEvents(c);

            // Cast the control to a MonthCalendar control.
            MonthCalendar monthCalendarControl = (MonthCalendar)c;

            // Remove the event.
            monthCalendarControl.DateChanged -=
                new DateRangeEventHandler(OnDateChanged);
        }

        // Declare the DateChanged event.
        public event DateRangeEventHandler DateChanged;

        // Raise the DateChanged event.
        private void OnDateChanged(object sender, DateRangeEventArgs e)
        {
            if (DateChanged != null)
            {
                DateChanged(this, e);
            }
        }
    }
}

⌨️ 快捷键说明

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