winform 日期控件

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1818728&SiteID=1
After more research on this, I figure out a better approach, which is to host the MonthCalendar control into a ToolStripControlHost object, then add the ToolStripControlHost object into the Items collection of a ToolStripDropDown.
So, the sample code would be something like this:


public
partial class ZhixinYeCalendarControl : UserControl

    {

        public ZhixinYeCalendarControl()

        {

            InitializeComponent();

            this.Load += new EventHandler(CalendarControl_Load);

        }

 

        private bool canDropDown;

        private TextBox textBox1;

        private Button button1;

        private MonthCalendar calendar;

 

        ToolStripDropDown popup;

 

        void CalendarControl_Load(object sender, EventArgs e)

        {

            this.canDropDown = true;

 

            this.calendar = new MonthCalendar();

            this.calendar.DateSelected += new DateRangeEventHandler(calendar_DateSelected);

 

            popup = new ToolStripDropDown();

           popup.Margin = Padding.Empty;

           popup.Padding = Padding.Empty;

           ToolStripControlHost host = new ToolStripControlHost(this.calendar);

           host.Margin = Padding.Empty;

           host.Padding = Padding.Empty;

           popup.Items.Add(host);

 

            this.Width = this.calendar.Width;

            this.Height = 21;

 

            this.textBox1 = new TextBox();

            this.textBox1.Left = 0;

            this.textBox1.Top = 0;

            this.textBox1.Width = this.Width - 20;

            this.textBox1.Height = 21;

            this.textBox1.Text = DateTime.Today.ToString();

            this.Controls.Add(this.textBox1);

 

            this.button1 = new Button();

            this.button1.Left = this.textBox1.Width;

            this.button1.Top = 0;

            this.button1.Width = 20;

            this.button1.Height = 21;

            this.button1.Paint += new PaintEventHandler(button1_Paint);

            this.button1.Click += new EventHandler(button1_Click);

            this.Controls.Add(this.button1);

        }

 

        void calendar_DateSelected(object sender, DateRangeEventArgs e)

        {

            this.textBox1.Text = e.Start.ToString();

            this.canDropDown = true;

            this.popup.Hide();

        }

 

        void button1_Paint(object sender, PaintEventArgs e)

        {

            ControlPaint.DrawComboButton(e.Graphics,

                this.button1.ClientRectangle,

                ButtonState.Normal);

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            if (canDropDown)

            {

                Point location;

              location = this.Parent.PointToScreen(this.Location);

              location = this.FindForm().PointToClient(location);

              location.Y = location.Y + this.Height;

              this.popup.Show(this.FindForm(),location);

            }

            else

            {

                this.popup.Hide();

            }

            canDropDown = !canDropDown;

        }

    }

原文地址:https://www.cnblogs.com/lgzh3/p/826773.html