zoukankan      html  css  js  c++  java
  • 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;

            }

        }

  • 相关阅读:
    全站生成静态文件的通用方法
    Web.config配置文件详解(新手必看)
    iis7/7.5设置上传文件最大大小
    C# 中的常用正则表达式总结
    60款很酷的 jQuery 幻灯片演示和下载
    DataReader记录生成多列的表格
    正则表达式
    博客转移
    ASP.Net学习之常用SQL存储过程(1)
    遍历Request的信息
  • 原文地址:https://www.cnblogs.com/lgzh3/p/826773.html
Copyright © 2011-2022 走看看