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