zoukankan      html  css  js  c++  java
  • DateChooser源码DateTimeStringEditor.cs

    using System;
    using System.ComponentModel;
    using System.Drawing.Design;
    using System.Security.Permissions;
    using System.Windows.Forms;
    using System.Windows.Forms.Design;
    namespace CNBlogs.DCT.THIN.Design
    {
     [SecurityPermission(SecurityAction.Demand, UnmanagedCode=true)]
     public class DateTimeStringEditor : UITypeEditor
     {
      // Methods
      public DateTimeStringEditor()
      {
      }

      public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
      {
       if (provider != null)
       {
        IWindowsFormsEditorService service1 = (IWindowsFormsEditorService) provider.GetService(typeof(IWindowsFormsEditorService));
        if (service1 == null)
        {
         return value;
        }
        if (this.dateTimeUI == null)
        {
         this.dateTimeUI = new DateTimeUI();
        }
        this.dateTimeUI.Start(service1, value);
        service1.DropDownControl(this.dateTimeUI);
        value = this.dateTimeUI.Value;
        this.dateTimeUI.End();
       }
       if(value is DateTime)
        return ((DateTime)value).ToString("yyyy-MM-dd");
       else
        return value.ToString();
      }

      public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
      {
       return UITypeEditorEditStyle.DropDown;
      }


      // Fields
      private DateTimeUI dateTimeUI;

      // Nested Types
      private class DateTimeUI : Control
      {
       // Methods
       public DateTimeUI()
       {
        this.btnClear = new Button();
        this.monthCalendar = new DateTimeMonthCalendar();
        this.InitializeComponent();
        base.Size = new System.Drawing.Size(this.monthCalendar.SingleMonthSize.Width,this.monthCalendar.SingleMonthSize.Height + 30);
        this.monthCalendar.Resize += new EventHandler(this.MonthCalResize);
        

       }

       public void End()
       {
        this.edSvc = null;
        this.value = null;
       }

       private void InitializeComponent()
       {
        this.btnClear.Text = "清空";
        //this.btnClear.Anchor = AnchorStyles.Bottom;
        this.btnClear.BackColor = System.Drawing.Color.MidnightBlue;
        this.btnClear.ForeColor = System.Drawing.Color.White;
        this.btnClear.Dock = DockStyle.Bottom;
        this.btnClear.Click += new EventHandler(this.OnbtnClearClick);

        //this.monthCalendar.MinDate = DateTime.MinValue;
        this.monthCalendar.DateSelected += new DateRangeEventHandler(this.OnDateSelected);
        this.monthCalendar.KeyDown += new KeyEventHandler(this.MonthCalKeyDown);
        base.Controls.Add(this.btnClear);
        base.Controls.Add(this.monthCalendar);
       }

       private void MonthCalKeyDown(object sender, KeyEventArgs e)
       {
        if (e.KeyCode == Keys.Return)
        {
         this.OnDateSelected(sender, null);
        }
       }

       private void MonthCalResize(object sender, EventArgs e)
       {
        base.Size = new System.Drawing.Size(this.monthCalendar.Size.Width,this.monthCalendar.Size.Height + 30);
        
       }
       private void OnbtnClearClick(object sender,EventArgs e)
       {
        this.value = "";
        this.edSvc.CloseDropDown();
       }

       private void OnDateSelected(object sender, DateRangeEventArgs e)
       {
        this.value = this.monthCalendar.SelectionStart;
        this.edSvc.CloseDropDown();
       }

       protected override void OnGotFocus(EventArgs e)
       {
        base.OnGotFocus(e);
        this.monthCalendar.Focus();
       }

       public void Start(IWindowsFormsEditorService edSvc, object value)
       {
        this.edSvc = edSvc;
        this.value = value;
        if (value != null)
        {
         DateTime time1 = DateTime.Today;
         try
         {
          time1 = DateTime.Parse(value.ToString());
         }
         catch
         {
         }
         if(time1 < monthCalendar.MinDate)
          this.monthCalendar.SetDate(this.monthCalendar.MinDate);
         else if(time1 > monthCalendar.MaxDate)
          this.monthCalendar.SetDate(monthCalendar.MaxDate);
         else
          this.monthCalendar.SetDate(time1);

        }
       }


       // Properties
       public object Value
       {
        get
        {
         return this.value;
        }
       }


       // Fields
       private IWindowsFormsEditorService edSvc;
       private Button btnClear;
       private MonthCalendar monthCalendar;
       private object value;

       // Nested Types
       private class DateTimeMonthCalendar : MonthCalendar
       {
        // Methods
        public DateTimeMonthCalendar()
        {
        }

        protected override bool IsInputKey(Keys keyData)
        {
         Keys keys1 = keyData;
         if (keys1 == Keys.Return)
         {
          return true;
         }
         return base.IsInputKey(keyData);
        }

       }
      }
     }
    }

  • 相关阅读:
    Kubernetes——安装GlusterFS分布式文件系统
    Jenkins从入门到精通——SonarQube:持续代码质量检查
    Jenkins从入门到精通——代码质量
    Redis从入门到精通——Redis数据类型——1、字符串(String)命令
    Redis从入门到精通——Redis客户端
    并发查询ElasticSearch, 根据分片来实现
    Golang开源定时任务调度框架robfig/cron优化
    解决Ubuntu17.04以上系统,yarn init报错
    Angularjs中的$q详解
    总结那些有默认margin,padding值的html标签
  • 原文地址:https://www.cnblogs.com/think/p/170131.html
Copyright © 2011-2022 走看看