C# 自定义控件,日期时间选择输入插件

[csharp] view plain copy
 
  1. // 为textBox1添加一个日期时间选择控件  
  2. DateTimeChoser.AddTo(textBox1);  

DateTimeChoser.Designer.cs

[csharp] view plain copy
 
  1. using System;  
  2. using System.ComponentModel;  
  3. using System.Drawing;  
  4. using System.Reflection;  
  5. using System.Windows.Forms;  
  6. namespace pictureAnalyse  
  7. {  
  8.     /// <summary>    
  9.     /// 此类用于实现一个日期时间辅助输入插件,调用逻辑:    
  10.     /// new DateTimeChoser(textBox1);  //即可为textBox1绑定一个日期时间输入控件    
  11.     /// </summary>    
  12.     public partial class DateTimeChoser : UserControl  
  13.     {  
  14.         public static bool showConfirmButton = true;   // 日期时间选择时,是否显示确定按钮    
  15.   
  16.         /// <summary>    
  17.         /// 为textBoox添加一个日期时间选择控件,辅助日期时间的输入    
  18.         /// </summary>    
  19.         public static void AddTo(TextBox textBox)  
  20.         {  
  21.             try  
  22.             {  
  23.                 DateTime time = DateTime.Parse(textBox.Text);  
  24.             }  
  25.             catch (Exception ex)  
  26.             {  
  27.                 textBox.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");  
  28.             }  
  29.   
  30.             textBox.MouseClick += textBoox_MouseClick;  
  31.         }  
  32.   
  33.         /// <summary>    
  34.         /// 为textBoox添加一个日期时间选择控件,辅助日期时间的输入,并设置初始时显示的时间    
  35.         /// </summary>    
  36.         public static void AddTo(TextBox textBoox, DateTime dateTime)  
  37.         {  
  38.             textBoox.Text = dateTime.ToString("yyyy-MM-dd HH:mm:ss");  
  39.             textBoox.MouseClick += textBoox_MouseClick;  
  40.         }  
  41.   
  42.         private static void textBoox_MouseClick(object sender, MouseEventArgs e)  
  43.         {  
  44.             TextBox textBox = sender as TextBox;  
  45.   
  46.             // 创建一个关联到textBox的日期时间选择控件    
  47.             DateTimeChoser choser = new DateTimeChoser();  
  48.             choser.showOn(textBox);  
  49.   
  50.             // 设置显示的时间为文本框中的日期时间    
  51.             try  
  52.             {  
  53.                 DateTime time = DateTime.Parse(textBox.Text);  
  54.                 choser.setDateTime(time);  
  55.             }  
  56.             catch (Exception ex) { }  
  57.         }  
  58.   
  59.         public DateTimeChoser()  
  60.         {  
  61.             InitializeComponent();  
  62.             init();  
  63.         }  
  64.   
  65.         private void init()  
  66.         {  
  67.             // 时分秒设置    
  68.             for (int i = 0; i < 24; i++) comboBox_hour.Items.Add((i < 10 ? "0" : "") + i);  
  69.             for (int i = 0; i < 60; i = i + 1) comboBox_minite.Items.Add((i < 10 ? "0" : "") + i);  
  70.             for (int i = 0; i < 60; i++) comboBox_second.Items.Add((i < 10 ? "0" : "") + i);  
  71.   
  72.             comboBox_hour.DropDownStyle = ComboBoxStyle.DropDownList;  
  73.             comboBox_minite.DropDownStyle = ComboBoxStyle.DropDownList;  
  74.             comboBox_second.DropDownStyle = ComboBoxStyle.DropDownList;  
  75.   
  76.             //设置显示的日期时间    
  77.             setDateTime(DateTime.Now);  
  78.         }  
  79.   
  80.         public delegate void DateTimeChanged_Handle(object sender, EventArgs e);  
  81.         [Description("当选择日期或时间变动时,调用此事件"), Category("自定义事件")]  
  82.         public event DateTimeChanged_Handle DateTimeChanged;  
  83.   
  84.         // 选择日期变动    
  85.         private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)  
  86.         {  
  87.             DateTime S = monthCalendar1.SelectionStart;  
  88.             string date = S.ToString("yyyy-MM-dd");  
  89.             if (!date.Equals(label_date.Text))  
  90.             {  
  91.                 label_date.Text = date;  
  92.                 if (DateTimeChanged != null) DateTimeChanged(this, new EventArgs());  
  93.             }  
  94.         }  
  95.   
  96.         //选择的时间变动    
  97.         private void TimeChanged(object sender, EventArgs e)  
  98.         {  
  99.             string time = comboBox_hour.Text + ":" + comboBox_minite.Text + ":" + comboBox_second.Text;  
  100.             if (!time.Equals(label_time.Text))  
  101.             {  
  102.                 label_time.Text = time;  
  103.                 if (DateTimeChanged != null) DateTimeChanged(this, new EventArgs());  
  104.             }  
  105.         }  
  106.   
  107.         // 设置显示到指定的日期时间    
  108.         public void setDateTime(DateTime now)  
  109.         {  
  110.             // 初始时界面显示当前的日期时间    
  111.             label_date.Text = now.ToString("yyyy-MM-dd");  
  112.             monthCalendar1.SetDate(now);  
  113.   
  114.             // 设置时间    
  115.             label_time.Text = now.ToString("HH:mm:ss");  
  116.             comboBox_hour.SelectedIndex = now.Hour;  
  117.             comboBox_minite.SelectedIndex = now.Minute;  
  118.             comboBox_second.SelectedIndex = now.Second;  
  119.         }  
  120.   
  121.         // 获取当前选择的日期时间    
  122.         public string getDateTime()  
  123.         {  
  124.             return label_date.Text + " " + label_time.Text;  
  125.         }  
  126.  
  127.  
  128.         # region 自定义控件输入绑定逻辑,将当前日期时间控件绑定到指定的TextBox  
  129.   
  130.         private Form form;  
  131.         TextBox textbox;  
  132.         private Delegate[] textboxEvents;  
  133.   
  134.         // 在指定的TextBox中,显示当前日期时间选择控件,进行日期时间的输入    
  135.         public void showOn(TextBox textBox, int offX = 0, int offY = 0)  
  136.         {  
  137.             Point P = getLocation(textBox);  
  138.             P = new Point(P.X, P.Y + textBox.Height);  
  139.   
  140.             show(textBox, P.X + offX, P.Y + offY, showConfirmButton);  
  141.         }  
  142.   
  143.         // 在TextBox点击时,调用DateTimeChoser进行日期时间的选择,当再次点击时,关闭之前的日期选择状态    
  144.         private void show(TextBox textbox, int L, int T, bool showButton)  
  145.         {  
  146.             this.textbox = textbox;  
  147.             textboxEvents = getEvents(textbox, "MouseClick");  // 获取TextBox原有事件处理逻辑    
  148.             ClearEvent(textbox, "MouseClick");          // 移除TextBox原有MouseClick事件处理逻辑    
  149.   
  150.             // 新建一个窗体    
  151.             form = new Form();  
  152.             form.Width = this.Width;  
  153.             form.Height = this.Height;  
  154.             if (showButton) form.Height = this.Height + 40;  
  155.             form.FormBorderStyle = FormBorderStyle.None;    // 无边框    
  156.             form.ShowInTaskbar = false;                     // 不在任务栏中显示    
  157.             form.BackColor = Color.White;                   //     
  158.   
  159.             form.Location = new Point(L, T);  
  160.   
  161.             // 将当前日期时间选择控件添加到form中    
  162.             this.Left = 0; this.Top = 0;  
  163.             form.Controls.Add(this);  
  164.   
  165.             if (showButton)  
  166.             {  
  167.                 Button button = new Button();  
  168.                 button.Text = "确定";  
  169.                 button.ForeColor = Color.Blue;  
  170.                 button.Left = (this.Width - button.Width) / 2;  
  171.                 button.Top = this.Height + (40 - button.Height) / 2;  
  172.                 form.Controls.Add(button);  
  173.   
  174.                 button.Click += button_Click;  
  175.             }  
  176.   
  177.             form.Show();             // 显示日期时间选择    
  178.             form.Location = new Point(L, T);  
  179.             form.TopMost = true;  
  180.             form.Activate();         // 当前界面获取到焦点    
  181.   
  182.             Form Parent = getParentForm(textbox);       // 获取TextBox的父窗体    
  183.             if (Parent != null) Parent.FormClosed += Parent_FormClosed;  
  184.   
  185.             textbox.MouseClick += textbox_MouseClick;  
  186.         }  
  187.   
  188.         // 添加    
  189.         private void button_Click(object sender, EventArgs e)  
  190.         {  
  191.             textbox_MouseClick(textbox, null);  
  192.         }  
  193.   
  194.         // 关闭当前form    
  195.         private void Parent_FormClosed(object sender, FormClosedEventArgs e)  
  196.         {  
  197.             if (form != null)  
  198.             {  
  199.                 form.Close();  
  200.                 form = null;  
  201.             }  
  202.         }  
  203.   
  204.         private void textbox_MouseClick(object sender, MouseEventArgs e)  
  205.         {  
  206.             TextBox textBox = sender as TextBox;  
  207.             textBox.Text = getDateTime();  
  208.   
  209.             if (form != null)  
  210.             {  
  211.                 form.Close();  
  212.                 form = null;  
  213.             }  
  214.   
  215.             textBox.MouseClick -= textbox_MouseClick;           // 移除当前事件处理逻辑    
  216.             addEvents(textBox, "MouseClick", textboxEvents);    // 还原TextBox原有事件处理逻辑    
  217.         }  
  218.  
  219.         # endregion  
  220.   
  221.   
  222.         // 获取给定控件的父窗体    
  223.         public static Form getParentForm(Control control)  
  224.         {  
  225.             if (control is Form) return control as Form;  
  226.   
  227.             Control C = control;  
  228.             while (C.Parent != null)  
  229.             {  
  230.                 if (C.Parent is Form) return C.Parent as Form;  
  231.                 else C = C.Parent;  
  232.             }  
  233.   
  234.             return null;  
  235.         }  
  236.  
  237.         #region 获取控件的坐标信息  
  238.   
  239.         /// <summary>    
  240.         /// 获取任意控件相对于屏幕的坐标    
  241.         /// </summary>    
  242.         public static Point getLocation(Control control)  
  243.         {  
  244.             Point P;  
  245.             if (control is Form) P = getFormClientLocation(control as Form);  
  246.             else P = control.Location;  
  247.             if (control.Parent != null)  
  248.             {  
  249.                 Control parent = control.Parent;  
  250.                 Point P2 = getLocation(parent);  
  251.   
  252.                 P = new Point(P.X + P2.X, P.Y + P2.Y);  
  253.             }  
  254.   
  255.             return P;  
  256.         }  
  257.   
  258.         /// <summary>    
  259.         /// 获取Form窗体有效显示区域的起点,相对于屏幕的坐标    
  260.         /// </summary>    
  261.         public static Point getFormClientLocation(Form form)  
  262.         {  
  263.             Rectangle rect = form.ClientRectangle;  
  264.   
  265.             int offx = 0, offy = 0;  
  266.             if (form.FormBorderStyle != FormBorderStyle.None)  
  267.             {  
  268.                 offx = (form.Width - rect.Width) / 2;  
  269.                 offy = (form.Height - rect.Height) - 8;  
  270.             }  
  271.   
  272.             Point P = new Point(form.Location.X + offx, form.Location.Y + offy);  
  273.   
  274.             return P;  
  275.         }  
  276.  
  277.         # endregion  
  278.  
  279.  
  280.         # region 动态修改控件对应的事件处理逻辑  
  281.   
  282.         // ClearEvent(button1,"Click");//就会清除button1对象的Click事件的所有挂接事件。    
  283.         private void ClearEvent(Control control, string eventname)  
  284.         {  
  285.             if (control == null) return;  
  286.             if (string.IsNullOrEmpty(eventname)) return;  
  287.             BindingFlags mPropertyFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic;  
  288.             BindingFlags mFieldFlags = BindingFlags.Static | BindingFlags.NonPublic;  
  289.             Type controlType = typeof(System.Windows.Forms.Control);  
  290.             PropertyInfo propertyInfo = controlType.GetProperty("Events", mPropertyFlags);  
  291.             EventHandlerList eventHandlerList = (EventHandlerList)propertyInfo.GetValue(control, null);  
  292.             FieldInfo fieldInfo = (typeof(Control)).GetField("Event" + eventname, mFieldFlags);  
  293.             Delegate d = eventHandlerList[fieldInfo.GetValue(control)];  
  294.             if (d == null) return;  
  295.             EventInfo eventInfo = controlType.GetEvent(eventname);  
  296.             foreach (Delegate dx in d.GetInvocationList())  
  297.                 eventInfo.RemoveEventHandler(control, dx);  
  298.         }  
  299.   
  300.         // getEvent(button1,"Click"); //就会获取到button1对象的Click事件的所有挂接事件。    
  301.         private Delegate[] getEvents(Control control, string eventname)  
  302.         {  
  303.             if (control == null) return null;  
  304.             if (string.IsNullOrEmpty(eventname)) return null;  
  305.             BindingFlags mPropertyFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic;  
  306.             BindingFlags mFieldFlags = BindingFlags.Static | BindingFlags.NonPublic;  
  307.             Type controlType = typeof(System.Windows.Forms.Control);  
  308.             PropertyInfo propertyInfo = controlType.GetProperty("Events", mPropertyFlags);  
  309.             EventHandlerList eventHandlerList = (EventHandlerList)propertyInfo.GetValue(control, null);  
  310.             FieldInfo fieldInfo = (typeof(Control)).GetField("Event" + eventname, mFieldFlags);  
  311.             Delegate d = eventHandlerList[fieldInfo.GetValue(control)];  
  312.             if (d == null) return null;  
  313.   
  314.             Delegate[] events = new Delegate[d.GetInvocationList().Length];  
  315.             int i = 0;  
  316.             foreach (Delegate dx in d.GetInvocationList()) events[i++] = dx;  
  317.   
  318.             return events;  
  319.         }  
  320.   
  321.         // addEvents(button1,"Click"); // 为button1对象的Click事件挂接事件    
  322.         private void addEvents(Control control, string eventname, Delegate[] evenets)  
  323.         {  
  324.             if (control == null) return;  
  325.             if (string.IsNullOrEmpty(eventname)) return;  
  326.   
  327.             Type controlType = typeof(System.Windows.Forms.Control);  
  328.             EventInfo eventInfo = controlType.GetEvent(eventname);  
  329.   
  330.             foreach (Delegate e in evenets)  
  331.                 eventInfo.AddEventHandler(control, e);  
  332.         }  
  333.  
  334.         # endregion  
  335.   
  336.     }  
  337.   
  338. }  
DateTimeChoser.cs
[csharp] view plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Drawing;  
  5. using System.Data;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10.   
  11. namespace pictureAnalyse  
  12. {  
  13.     partial class DateTimeChoser  
  14.     {  
  15.         /// <summary>   
  16.         /// 必需的设计器变量。  
  17.         /// </summary>  
  18.         private System.ComponentModel.IContainer components = null;  
  19.   
  20.         /// <summary>   
  21.         /// 清理所有正在使用的资源。  
  22.         /// </summary>  
  23.         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>  
  24.         protected override void Dispose(bool disposing)  
  25.         {  
  26.             if (disposing && (components != null))  
  27.             {  
  28.                 components.Dispose();  
  29.             }  
  30.             base.Dispose(disposing);  
  31.         }  
  32.  
  33.         #region 组件设计器生成的代码  
  34.   
  35.         /// <summary>   
  36.         /// 设计器支持所需的方法 - 不要  
  37.         /// 使用代码编辑器修改此方法的内容。  
  38.         /// </summary>  
  39.         private void InitializeComponent()  
  40.         {  
  41.             this.monthCalendar1 = new System.Windows.Forms.MonthCalendar();  
  42.             this.panel_consume = new System.Windows.Forms.Panel();  
  43.             this.label6 = new System.Windows.Forms.Label();  
  44.             this.comboBox_second = new System.Windows.Forms.ComboBox();  
  45.             this.label5 = new System.Windows.Forms.Label();  
  46.             this.comboBox_minite = new System.Windows.Forms.ComboBox();  
  47.             this.label4 = new System.Windows.Forms.Label();  
  48.             this.comboBox_hour = new System.Windows.Forms.ComboBox();  
  49.             this.panel1 = new System.Windows.Forms.Panel();  
  50.             this.label_date = new System.Windows.Forms.Label();  
  51.             this.label_time = new System.Windows.Forms.Label();  
  52.             this.panel2 = new System.Windows.Forms.Panel();  
  53.             this.panel_consume.SuspendLayout();  
  54.             this.panel1.SuspendLayout();  
  55.             this.panel2.SuspendLayout();  
  56.             this.SuspendLayout();  
  57.             //   
  58.             // monthCalendar1  
  59.             //   
  60.             this.monthCalendar1.AllowDrop = true;  
  61.             this.monthCalendar1.Location = new System.Drawing.Point(-3, 15);  
  62.             this.monthCalendar1.Name = "monthCalendar1";  
  63.             this.monthCalendar1.TabIndex = 0;  
  64.             this.monthCalendar1.DateSelected += new System.Windows.Forms.DateRangeEventHandler(this.monthCalendar1_DateSelected);  
  65.             //   
  66.             // panel_consume  
  67.             //   
  68.             this.panel_consume.BackColor = System.Drawing.Color.White;  
  69.             this.panel_consume.Controls.Add(this.label6);  
  70.             this.panel_consume.Controls.Add(this.comboBox_second);  
  71.             this.panel_consume.Controls.Add(this.label5);  
  72.             this.panel_consume.Controls.Add(this.comboBox_minite);  
  73.             this.panel_consume.Controls.Add(this.label4);  
  74.             this.panel_consume.Controls.Add(this.comboBox_hour);  
  75.             this.panel_consume.Location = new System.Drawing.Point(-2, 173);  
  76.             this.panel_consume.Name = "panel_consume";  
  77.             this.panel_consume.Size = new System.Drawing.Size(222, 30);  
  78.             this.panel_consume.TabIndex = 23;  
  79.             //   
  80.             // label6  
  81.             //   
  82.             this.label6.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));  
  83.             this.label6.AutoSize = true;  
  84.             this.label6.Location = new System.Drawing.Point(195, 10);  
  85.             this.label6.Name = "label6";  
  86.             this.label6.Size = new System.Drawing.Size(17, 12);  
  87.             this.label6.TabIndex = 15;  
  88.             this.label6.Text = "秒";  
  89.             //   
  90.             // comboBox_second  
  91.             //   
  92.             this.comboBox_second.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));  
  93.             this.comboBox_second.FormattingEnabled = true;  
  94.             this.comboBox_second.Location = new System.Drawing.Point(149, 6);  
  95.             this.comboBox_second.Name = "comboBox_second";  
  96.             this.comboBox_second.Size = new System.Drawing.Size(40, 20);  
  97.             this.comboBox_second.TabIndex = 14;  
  98.             this.comboBox_second.Text = "0";  
  99.             this.comboBox_second.SelectedIndexChanged += new System.EventHandler(this.TimeChanged);  
  100.             //   
  101.             // label5  
  102.             //   
  103.             this.label5.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));  
  104.             this.label5.AutoSize = true;  
  105.             this.label5.Location = new System.Drawing.Point(126, 10);  
  106.             this.label5.Name = "label5";  
  107.             this.label5.Size = new System.Drawing.Size(17, 12);  
  108.             this.label5.TabIndex = 13;  
  109.             this.label5.Text = "分";  
  110.             //   
  111.             // comboBox_minite  
  112.             //   
  113.             this.comboBox_minite.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));  
  114.             this.comboBox_minite.FormattingEnabled = true;  
  115.             this.comboBox_minite.Location = new System.Drawing.Point(80, 6);  
  116.             this.comboBox_minite.Name = "comboBox_minite";  
  117.             this.comboBox_minite.Size = new System.Drawing.Size(40, 20);  
  118.             this.comboBox_minite.TabIndex = 12;  
  119.             this.comboBox_minite.Text = "0";  
  120.             this.comboBox_minite.SelectedIndexChanged += new System.EventHandler(this.TimeChanged);  
  121.             //   
  122.             // label4  
  123.             //   
  124.             this.label4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));  
  125.             this.label4.AutoSize = true;  
  126.             this.label4.Location = new System.Drawing.Point(57, 10);  
  127.             this.label4.Name = "label4";  
  128.             this.label4.Size = new System.Drawing.Size(17, 12);  
  129.             this.label4.TabIndex = 11;  
  130.             this.label4.Text = "时";  
  131.             //   
  132.             // comboBox_hour  
  133.             //   
  134.             this.comboBox_hour.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));  
  135.             this.comboBox_hour.FormattingEnabled = true;  
  136.             this.comboBox_hour.Location = new System.Drawing.Point(9, 6);  
  137.             this.comboBox_hour.Name = "comboBox_hour";  
  138.             this.comboBox_hour.Size = new System.Drawing.Size(42, 20);  
  139.             this.comboBox_hour.TabIndex = 10;  
  140.             this.comboBox_hour.Text = "0";  
  141.             this.comboBox_hour.SelectedIndexChanged += new System.EventHandler(this.TimeChanged);  
  142.             //   
  143.             // panel1  
  144.             //   
  145.             this.panel1.BackColor = System.Drawing.Color.White;  
  146.             this.panel1.Controls.Add(this.label_date);  
  147.             this.panel1.Controls.Add(this.label_time);  
  148.             this.panel1.Location = new System.Drawing.Point(-3, -1);  
  149.             this.panel1.Name = "panel1";  
  150.             this.panel1.Size = new System.Drawing.Size(223, 23);  
  151.             this.panel1.TabIndex = 25;  
  152.             //   
  153.             // label_date  
  154.             //   
  155.             this.label_date.AutoSize = true;  
  156.             this.label_date.BackColor = System.Drawing.Color.White;  
  157.             this.label_date.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));  
  158.             this.label_date.Location = new System.Drawing.Point(18, 3);  
  159.             this.label_date.Name = "label_date";  
  160.             this.label_date.Size = new System.Drawing.Size(98, 16);  
  161.             this.label_date.TabIndex = 26;  
  162.             this.label_date.Text = "2016-06-12";  
  163.             //   
  164.             // label_time  
  165.             //   
  166.             this.label_time.AutoSize = true;  
  167.             this.label_time.BackColor = System.Drawing.Color.White;  
  168.             this.label_time.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));  
  169.             this.label_time.Location = new System.Drawing.Point(118, 3);  
  170.             this.label_time.Name = "label_time";  
  171.             this.label_time.Size = new System.Drawing.Size(80, 16);  
  172.             this.label_time.TabIndex = 25;  
  173.             this.label_time.Text = "12:23:35";  
  174.             //   
  175.             // panel2  
  176.             //   
  177.             this.panel2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;  
  178.             this.panel2.Controls.Add(this.panel1);  
  179.             this.panel2.Controls.Add(this.panel_consume);  
  180.             this.panel2.Controls.Add(this.monthCalendar1);  
  181.             this.panel2.Location = new System.Drawing.Point(0, 0);  
  182.             this.panel2.Name = "panel2";  
  183.             this.panel2.Size = new System.Drawing.Size(215, 202);  
  184.             this.panel2.TabIndex = 26;  
  185.             //   
  186.             // DateTimeChoser  
  187.             //   
  188.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);  
  189.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;  
  190.             this.Controls.Add(this.panel2);  
  191.             this.Name = "DateTimeChoser";  
  192.             this.Size = new System.Drawing.Size(215, 202);  
  193.             this.panel_consume.ResumeLayout(false);  
  194.             this.panel_consume.PerformLayout();  
  195.             this.panel1.ResumeLayout(false);  
  196.             this.panel1.PerformLayout();  
  197.             this.panel2.ResumeLayout(false);  
  198.             this.ResumeLayout(false);  
  199.   
  200.         }  
  201.  
  202.         #endregion  
  203.   
  204.         private System.Windows.Forms.MonthCalendar monthCalendar1;  
  205.         private System.Windows.Forms.Panel panel_consume;  
  206.         private System.Windows.Forms.Label label6;  
  207.         private System.Windows.Forms.ComboBox comboBox_second;  
  208.         private System.Windows.Forms.Label label5;  
  209.         private System.Windows.Forms.ComboBox comboBox_minite;  
  210.         private System.Windows.Forms.Label label4;  
  211.         private System.Windows.Forms.ComboBox comboBox_hour;  
  212.         private System.Windows.Forms.Panel panel1;  
  213.         private System.Windows.Forms.Label label_date;  
  214.         private System.Windows.Forms.Label label_time;  
  215.         private System.Windows.Forms.Panel panel2;  
  216.     }  
  217. }  

添加以上两个类到项目,编译运行一次。自定义的DateTimeChoser控件会出现在工具箱中。

可直接拖拽至Form窗体中使用,也可通过代码进行调用。

示例下载 pictureAnalyse.exe

示例源码下载 pictureAnalyse.zip

原文地址:https://www.cnblogs.com/Alex80/p/7380008.html