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);
        }

       }
      }
     }
    }

  • 相关阅读:
    SocketAsyncEventArgs的释放问题
    SharePoint 2013部署自定义HttpModule访问SPContext.Current的一个问题
    ASP.NET MVC View使用Conditional compilation symbols
    XPath注入
    Java基础(十二)之包和权限访问
    SSI注入漏洞
    java基础(十一)之抽象类和抽象函数
    邮件头注入
    java基础(十)之向上转型/向下转型
    java基础(八)之函数的复写/重写(override)
  • 原文地址:https://www.cnblogs.com/think/p/170131.html
Copyright © 2011-2022 走看看