C # Enter DateTimePicker V1.2 in Winform.

Source: Internet
Author: User

[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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.