[Csharp]
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Windows. Forms;
Using System. ComponentModel;
Namespace WindowsFormsApplication1.Common
{
/// <Summary>
/// Description: DateTimePicker control v1.2, which can be null.
/// Author: Li Weibo
/// Creation date:
/// Modification date: the modification date does not display the time BUG after the value assignment is modified on, And the deletion time is not displayed after the Panel is opened.
/// </Summary>
Public class DateTimePickerX: DateTimePicker
{
Public DateTimePickerX ()
: Base ()
{
SetNullText ();
}
/// <Summary>
/// Whether the value has changed
/// </Summary>
Private bool IsValueChanged = false;
/// <Summary>
/// Original Format String
/// </Summary>
Private string _ formatString = "MM dd, yyyy ";
/// <Summary>
/// Original format
/// </Summary>
Private DateTimePickerFormat _ originalFormat = DateTimePickerFormat. Custom;
/// <Summary>
/// Enter a date
/// </Summary>
Private DateTime? ValueX = null;
/// <Summary>
/// Date (null before value change)
/// </Summary>
[Browsable (true), Category ("extended property"), Description ("empty date allowed (null before value change)")]
Public DateTime? ValueX
{
Get {return valueX ;}
Set
{
If (value = null)
{
ValueX = value;
SetNullText ();
}
Else
{
This. CustomFormat = FormatString;
This. Value = value. Value;
ValueX = value;
}
}
}
/// <Summary>
/// Formatted string
/// </Summary>
[Browsable (true), Category ("extended property"), Description ("Format String")]
Public string FormatString
{
Get {return _ formatString ;}
Set {_ formatString = value ;}
}
/// <Summary>
/// Set the format
/// </Summary>
[Browsable (true), Category ("extended property"), Description ("set format")]
Public DateTimePickerFormat OriginalFormat
{
Get {return _ originalFormat ;}
Set {_ originalFormat = value ;}
}
/// <Summary>
/// Start time (00:00:00 of the date)
/// </Summary>
[Browsable (true), Category ("extended attribute"), Description ("Start Time (00:00:00 of the currently selected date)")]
Public DateTime? BeginTime
{
Get
{
If (ValueX. HasValue)
Return DateTime. Parse (ValueX. Value. ToString ("yyyy-MM-dd 00:00:00 "));
Else
Return null;
}
}
/// <Summary>
/// End Time (23:59:59 of the date)
/// </Summary>
[Browsable (true), Category ("extended attribute"), Description ("End Time (23:59:59 of the currently selected date)")]
Public DateTime? EndTime
{
Get
{
If (ValueX. HasValue)
{
Return DateTime. Parse (ValueX. Value. ToString ("yyyy-MM-dd 23:59:59 "));
}
Else
Return null;
}
}
/// <Summary>
/// Set the NULL Character Display
/// </Summary>
Private void SetNullText ()
{
This. Format = System. Windows. Forms. DateTimePickerFormat. Custom;
This. CustomFormat = "";
}
Protected override void OnCloseUp (EventArgs eventargs)
{
If (IsValueChanged)
{
This. Format = _ originalFormat;
This. CustomFormat = _ formatString;
ValueX = this. Value;
IsValueChanged = false;
}
Base. OnCloseUp (eventargs );
}
Protected override void OnValueChanged (EventArgs eventargs)
{
This. IsValueChanged = true;
Base. OnValueChanged (eventargs );
}
Protected override void OnKeyDown (KeyEventArgs e)
{
// Deletion time: www.2cto.com
If (e. KeyCode = Keys. Back | e. KeyCode = Keys. Delete)
{
This. ValueX = null;
This. CustomFormat = "";
}
Base. OnKeyDown (e );
}
}
}
Author: ranbolwb