扩展 DateTimePicker






3.58/5 (17投票s)
2003年1月29日
4分钟阅读

333785

8620
DateTime Picker 允许在未输入数据时显示为空。
引言
本文介绍对 MS DateTimePicker
的扩展。
背景
我开发此控件是为了满足客户的需求。在我正在开发的数据库应用程序中,我的客户要求我显示日期和时间为空,除非输入了有效的日期或时间。此外,他们希望显示一个单独的字段,其中包含输入的日期相关的星期几,以及带有相关 AM 或 PM 的日期,但没有具体的时间。
为了实现这一点,我构建了一个基于 DateTimePicker
类的复合控件。
该控件继承自 System.Windows.Forms.DateTimePicker
控件,我将一个 TextBox
添加到 DateTimePicker
中,使其位于日期时间显示区域的前面,并允许用户输入日期和/或时间,并根据所选格式显示日期和/或时间。控件中还包含了一个 Tooltip
对象和一个 ErrorProvider
。我可能在格式选项上有些过度,但其中一些比较奇怪的选项对于我按照客户要求显示数据是必要的。
首先,我重写了 DateTimePicker
的标准 Format
属性,将其设置为 Browsable
(false
) 并为其提供了一个单一的访问器 get
,它返回 base.Format
。标准 Format
属性在“属性”窗口中不再可用,并且无法在新的控件外部设置。
接下来,我定义了一个新的属性 FormatEx
,它是可浏览的,类型为 enum
dtpCustomExtensions
,这是一个枚举,包含了该控件可以处理的所有可能的日期和时间格式的标准。它们包括 dtpLong
、dtpShort
、dtpTime
和 dtpCustom
,它们提供与以前在 Format
属性中可选择的 Long
、Short
、Time
和 Custom
属性相同的重复功能。标准 Format
属性被永久设置为 Custom
。
InitialiseCustomMessage()
在 FormatEx
被设置为新值时被调用,InitialiseCustomMessage()
在 Tooltip
中设置适当的消息,当鼠标悬停在控件上时显示。
FormatTextBox()
在需要显示日期或时间在 TextBox
中时被调用。
有一些本地化,但由于所有消息都硬编码为英文,因此该控件实际上仅在讲英语的国家/地区有用。用户可以输入任何有效的日期(根据当前区域设置有效)到 TextBox
中,该日期将被解析并根据当前所选 FormatEx
值关联的格式规则重新显示。
已重写了许多标准的 DateTimePicker
属性,包括 ShowUpDown
和 ShowCheckBox
。我想确保当新的 ReadOnly
属性为 true
时,这些属性以及新的 ShowButtons
属性(用于隐藏/显示按钮)都被锁定为 false
。ReadOnly
设置/获取 TextBox
的 ReadOnly
属性,并作为将其暴露在属性窗口中,并使该功能可用于控件。
我添加了另一个属性 LinkTo
,它允许我将控件的一个实例与一个或多个 TAS.Widgets.DateTimePicker
控件的实例关联起来。使用此功能,可以使一个或多个控件的值与主实例同步,例如,我可以在一个控件中显示日期,而在另一个控件中显示星期几。
要将 TAS.Widgets.DateTimePicker
的其他实例链接到主实例,请在主实例的 LinkTo
属性中添加链接控件的名称,格式为 ChildControlName1
,ChildControlName2
,ChildControlName3
等。
使用代码
要运行演示应用程序,请下载 DTP_Example.zip,并将文件提取到某个目录中。演示应用程序 WindowsApplication2.exe 需要文件 DateTimePicker.dll。如果这两个文件未被提取,将会出现运行时错误 System.IO.FileNotFoundException has occurred in WindowsApplication2.exe
。
如果您觉得这个控件很有用,请创建一个名为 TracyAnneSofware(或您喜欢的任何名称)的目录并将其移入该目录,然后,如果您使用 Visual Studio IDE,请将其添加到工具箱中。
代码
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using TAS.Widgets;
namespace TAS.Widgets
{
public class DateTimePicker : System.Windows.Forms.DateTimePicker
{
private System.Windows.Forms.TextBox txtDateTime;
private System.ComponentModel.IContainer components;
private bool SetDate;
private System.Windows.Forms.ErrorProvider ErrorMessage;
private System.Windows.Forms.ToolTip Tooltip;
private const int BTNWIDTH = 16;
public enum dtpCustomExtensions
{
dtpLong = 0,
dtpShort = 1,
dtpTime = 2,
dtpShortDateShortTimeAMPM = 3,
dtpShortDateLongTimeAMPM = 4,
dtpShortDateShortTime24Hour = 5,
dtpShortDateLongTime24Hour = 6,
dtpLongDateShortTimeAMPM = 7,
dtpLongDateLongTimeAMPM = 8,
dtpLongDateShortTime24Hour = 9,
dtpLongDateLongTime24Hour = 10,
dtpSortableDateAndTimeLocalTime = 11,
dtpUTFLocalDateAndShortTimeAMPM = 12,
dtpUTFLocalDateAndLongTimeAMPM = 13,
dtpUTFLocalDateAndShortTime24Hour = 14,
dtpUTFLocalDateAndLongTime24Hour = 15,
dtpShortTimeAMPM = 16,
dtpShortTime24Hour = 17,
dtpLongTime24Hour = 18,
dtpYearAndMonthName = 19,
dtpMonthNameAndDay = 20,
dtpYear4Digit = 21,
dtpMonthFullName = 22,
dtpMonthShortName = 23,
dtpDayFullName = 24,
dtpDayShortName = 25,
dtpShortDateAMPM = 26,
dtpShortDateMorningAfternoon = 27,
dtpCustom = 28
}
private string mvarLinkedTo;
private bool bDroppedDown;
private int ButtonWidth = BTNWIDTH;
private bool mvarShowButtons = true;
private dtpCustomExtensions mvarFormatEx;
private string mvarCustomFormatMessage;
private int CheckWidth = 0;
private TAS.Widgets.DateTimePicker LinkTo;
private System.Collections.ArrayList
LinkToArray = new System.Collections.ArrayList();
private System.Collections.ArrayList
LinkedArray = new System.Collections.ArrayList();
#region Constructor and destructor
public DateTimePicker()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
// TODO: Add any initialization after the InitForm call
//Initialise bas.Format to Custom,
//we only need Custom Format
base.Format =
System.Windows.Forms.DateTimePickerFormat.Custom;
DateTimePicker_Resize(this, null);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}
#endregion Constructor and destructor
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.txtDateTime = new System.Windows.Forms.TextBox();
this.ErrorMessage = new System.Windows.Forms.ErrorProvider();
this.Tooltip = new
System.Windows.Forms.ToolTip(this.components);
this.SuspendLayout();
//
// txtDateTime
//
this.txtDateTime.Location = new System.Drawing.Point(20, 49);
this.txtDateTime.MaxLength = 50;
this.txtDateTime.Name = "txtDateTime";
this.txtDateTime.TabIndex = 0;
this.txtDateTime.Text = "";
this.txtDateTime.BackColorChanged +=
new System.EventHandler(this.txtDateTime_BackColorChanged);
this.txtDateTime.Leave +=
new System.EventHandler(this.txtDateTime_Leave);
this.txtDateTime.Enter +=
new System.EventHandler(this.txtDateTime_Enter);
//
// ErrorMessage
//
this.ErrorMessage.DataMember = null;
//
// DateTimePicker
//
this.Controls.AddRange(new System.Windows.Forms.Control[]
{this.txtDateTime});
this.Value = new System.DateTime(1753, 1, 1, 15, 8, 40, 119);
this.DropDown +=
new System.EventHandler(this.DateTimePicker_DropDown);
this.FontChanged +=
new System.EventHandler(this.DateTimePicker_FontChanged);
this.Resize +=
new System.EventHandler(this.DateTimePicker_Resize);
this.Enter +=
new System.EventHandler(this.DateTimePicker_Enter);
this.CloseUp +=
new System.EventHandler(this.DateTimePicker_CloseUp);
this.ForeColorChanged +=
new System.EventHandler
(this.DateTimePicker_ForeColorChanged);
this.BackColorChanged +=
new System.EventHandler
(this.DateTimePicker_BackColorChanged);
this.ValueChanged +=
new System.EventHandler(this.FormatOrValueChanged);
this.FormatChanged +=
new System.EventHandler(this.FormatOrValueChanged);
this.ResumeLayout(false);
}
#endregion
#region overriden and additional properties
//OverRide Formst and hide it by setting
//Browsable false, make it read only
//so it can't be written to, it will always be Custom anyway
[Browsable(false)]
public new System.Windows.Forms.DateTimePickerFormat Format
{
get
{
return base.Format;
}
//set
//{
// base.Format = value;
//}
}
//FormatEx, extends the formatting options
//by allowing additional selections
//Replaces Format
[Browsable(true), Category("Appearance"),
Description("Format Extensions replaces Format
gets sets display Formats")]
public dtpCustomExtensions FormatEx
{
get
{
return mvarFormatEx;
}
set
{
mvarFormatEx = value;
InitialiseCustomMessage();
}
}
//New Property, allows hiding of DropDown
//Button and Updown Button
[Browsable(true), Category("Appearance"),
Description("Hides DropDown and Spin Buttons,
Allows keyed entry only.")]
public bool ShowButtons
{
get
{
return mvarShowButtons;
}
set
{
//Do not allow Set Show Buttons when ReadOnly is true
//all Buttons and Chexkbox are hidden
//when Control is Read Only
if (!this.ReadOnly)
{
mvarShowButtons = value;
if (mvarShowButtons)
{
ButtonWidth = BTNWIDTH;
}
else
{
ButtonWidth = 0;
}
DateTimePicker_Resize(this, null);
}
}
}
//Overrides base.ShowCheckBox
[Browsable(true), Category("Appearance"),
Description("Hides DropDown and Spin Buttons,
Allows keyed entry only.")]
public new bool ShowCheckBox
{
get
{
return base.ShowCheckBox;
}
set
{
//Do not allow set ShowCheckBox when ReadOnly is True
//all Buttons and Chexkbox are hidden
//when Control is Read Only
if (!this.ReadOnly)
{
base.ShowCheckBox = value;
if (base.ShowCheckBox)
{
CheckWidth = BTNWIDTH;
}
else
{
CheckWidth = 0;
}
DateTimePicker_Resize(this,null);
}
}
}
//overrie Text, we want to set Get Textbox Text
[Browsable(true), Category("Behavior"),
Description("Date and Time displayed")]
public new string Text
{
get
{
return txtDateTime.Text;
}
set
{
txtDateTime.Text = value;
//Don't bother Formatting the Textbox
//if it's value is NullString
//It will cause problems if you do
if (value != "")
{
FormatTextBox();
}
}
}
//Override bas.ShowUpDown
[Browsable(true), Category("Appearance"),
Description("Uses Updown control to select
dates instead of Dropdown control")]
public new bool ShowUpDown
{
get
{
return base.ShowUpDown;
}
set
{
//Do not allow set ShowUpDown when ReadOnly is True
//all Buttons and Checkbox are
//hidden when Control is Read Only
if (!this.ReadOnly)
{
base.ShowUpDown = value;
txtDateTime.Text = "";
}
}
}
//Override Textbox back Colour so we can
//add it to the Appearance List
//and use it to set the BG colour
[Browsable(true), Category("Appearance"),
Description("The Backround Colour user to display
Text and Graphics in this Control")]
public new System.Drawing.Color BackColor
{
get
{
return base.BackColor;
}
set
{
base.BackColor = value;
}
}
//New Property Read Only makes it possible
//to set Textbox to read only
[Browsable(true), Category("Behavior"),
Description("Used to set whether the
control can be edited")]
public bool ReadOnly
{
get
{
return txtDateTime.ReadOnly;
}
set
{
//If ReadOnly is true make sure
//ShowCheckBox, ShowUpDown and ShowButtons
//are false.
//all Buttons and Checkbox are hidden
//when Control is Read Only
//Be aware of the order these properties are set
if (value)
{
this.ShowCheckBox = false;
this.ShowUpDown = false;
this.ShowButtons = false;
txtDateTime.ReadOnly = value;
}
else
{
txtDateTime.ReadOnly = value;
this.ShowButtons = true;
}
}
}
//New Property Makes it possible to link
//control to another Datetimepicker
[Browsable(true), Category("Behavior"),
Description("Set Get another Date Picker Control
that this control receives data from.")]
public string LinkedTo
{
get
{
return mvarLinkedTo;
}
set
{
mvarLinkedTo = value;
LinkedArray.Clear();
if (mvarLinkedTo != "" && mvarLinkedTo != null)
{
string[] splitmvarLinkedTo =
mvarLinkedTo.Split(",".ToCharArray());
for (int i = 0; i < splitmvarLinkedTo.Length; i++)
{
LinkedArray.Add(splitmvarLinkedTo[i].Trim());
}
}
}
}
#endregion
#region DateTimePicker events
private void DateTimePicker_Resize(object sender,
System.EventArgs e)
{
this.txtDateTime.Location = new
System.Drawing.Point(-2 + CheckWidth, -2);
this.txtDateTime.Size = new
System.Drawing.Size(this.Width - ButtonWidth - CheckWidth,
this.Height);
}
private void DateTimePicker_FontChanged(Object sender,
System.EventArgs e )
{
//Make sure TextBox Font = Dtp Font
txtDateTime.Font = this.Font;
}
private void DateTimePicker_BackColorChanged(Object sender,
System.EventArgs e)
{
//Make sure TextBox BackColour = Dtp Back Colour
txtDateTime.BackColor = this.BackColor;
}
private void txtDateTime_BackColorChanged(Object sender,
System.EventArgs e)
{
//Make sure DTP BackColour = TextBox Back Colour
if (txtDateTime.BackColor != this.BackColor)
{
this.BackColor = txtDateTime.BackColor;
}
}
private void DateTimePicker_ForeColorChanged(Object sender,
System.EventArgs e)
{
//Make sure TextBox Fore Colour = Dtp Fore Colour
txtDateTime.ForeColor = this.BackColor;
}
private void FormatOrValueChanged(Object sender,
System.EventArgs e)
{
ErrorMessage.SetError(this, "");
//if dtp Value changed
//Attempt to Format the TextBox String
//if Text is not NullString
if (this.Text != "" )
{
try
{
FormatTextBox();
}
catch
{
ErrorMessage.SetError(this, "Invalid Date - "
+ txtDateTime.Text + ", valid format is "
+ mvarCustomFormatMessage);
}
}
}
private void txtDateTime_Enter(Object sender,
System.EventArgs e )
{
Tooltip.SetToolTip(txtDateTime, mvarCustomFormatMessage);
if (txtDateTime.Text.Length > 0 )
{
txtDateTime.SelectionStart = 0;
txtDateTime.SelectionLength =
txtDateTime.Text.Length;
}
SetDate = true;
this.Value = DateTime.Now;
SetDate = false;
}
private void txtDateTime_Leave(Object sender,
System.EventArgs e )
{
if (! SetDate )
{
SetDate = true;
ErrorMessage.SetError(this, "");
//Attempt to Format the TextBox String
//if Text is not NullString
if (this.Text != "" )
{
try
{
FormatTextBox();
//if Link To is Not nullString
//Attempt to Link to the Specified LinkTo Controls
LinkToArray.Clear();
if (mvarLinkedTo != "" && mvarLinkedTo != null)
{
for (int j = 0; j < LinkedArray.Count; j++)
{
for (int i = 0;
i < this.Parent.Controls.Count; i++)
{
if (this.Parent.Controls[i].Name ==
LinkedArray[j].ToString() &&
this.Parent.Controls[i] is
TAS.Widgets.DateTimePicker)
{
LinkTo = (TAS.Widgets.DateTimePicker)
this.Parent.Controls[i];
LinkToArray.Add(LinkTo);
break;
}
}
}
}
}
catch
{
ErrorMessage.SetError(this, "Invalid Date - " +
txtDateTime.Text + ", valid format is " +
mvarCustomFormatMessage);
}
}
//IF the LinkTo Object has been instantiated it's
//ok to attempt to set it's Text Value
for (int i = 0; i < LinkToArray.Count; i++)
{
if (this.LinkToArray[i] != null)
{
LinkTo = (TAS.Widgets.DateTimePicker)
LinkToArray[i];
LinkTo.Text = this.Text;
}
}
SetDate = false;
}
}
private void DateTimePicker_Enter(Object sender,
System.EventArgs e)
{
txtDateTime.Focus();
}
private void DateTimePicker_DropDown(Object sender,
System.EventArgs e)
{
bDroppedDown = true;
}
private void DateTimePicker_CloseUp(object sender,
System.EventArgs e)
{
if (bDroppedDown || this.ShowUpDown )
{
if (! SetDate )
{
txtDateTime.Text = this.Value.ToString();
FormatTextBox();
bDroppedDown = false;
txtDateTime.Focus();
}
}
}
protected override void
OnValueChanged(System.EventArgs eventargs)
{
if (bDroppedDown || this.ShowUpDown )
{
if (! SetDate )
{
txtDateTime.Text = this.Value.ToString();
FormatTextBox();
}
}
}
//Set up the message that will diplay in the Tooltip
//when the mouse is hovered over the control
private void InitialiseCustomMessage()
{
switch (mvarFormatEx)
{
case dtpCustomExtensions.dtpCustom:
mvarCustomFormatMessage = this.CustomFormat;
break;
case dtpCustomExtensions.dtpLong:
mvarCustomFormatMessage = "Long Date (" +
DateTime.Now.ToLongDateString() + ")";
this.CustomFormat = "yyyy-MM-dd HH:mm:ss";
break;
case dtpCustomExtensions.dtpShort:
mvarCustomFormatMessage = "Short Date (" +
DateTime.Now.ToShortDateString() + ")";
this.CustomFormat = "yyyy-MM-dd HH:mm:ss";
break;
case dtpCustomExtensions.dtpTime:
mvarCustomFormatMessage = "Long Time AM/PM (" +
DateTime.Now.ToLongTimeString() + ")";
this.CustomFormat = "HH:mm:ss yyyy-MM-dd ";
break;
case dtpCustomExtensions.dtpDayFullName:
mvarCustomFormatMessage =
"Day of the Week Full Name (" +
DateTime.Now.ToString("dddd",
Application.CurrentCulture) + ")";
this.CustomFormat = "dd-MM-yyyy HH:mm:ss";
break;
case dtpCustomExtensions.dtpDayShortName:
mvarCustomFormatMessage =
"Day of the Week Short Name ("
+ DateTime.Now.ToString("ddd",
Application.CurrentCulture) + ")";
this.CustomFormat = "dd-MM-yyyy HH:mm:ss";
break;
case dtpCustomExtensions.dtpLongDateLongTime24Hour:
mvarCustomFormatMessage =
"Long Date Long Time 24 Hour (" +
DateTime.Now.ToString("D",
Application.CurrentCulture) + " " +
DateTime.Now.ToString("HH:mm:ss",
Application.CurrentCulture) + ")";
this.CustomFormat = "yyyy-MM-dd HH:mm:ss";
break;
case dtpCustomExtensions.dtpLongDateLongTimeAMPM:
mvarCustomFormatMessage =
"Long Date Long Time AM/PM (" +
DateTime.Now.ToString("D",
Application.CurrentCulture) + " " +
DateTime.Now.ToString("hh:mm:ss tt",
Application.CurrentCulture) + ")";
this.CustomFormat = "yyyy-MM-dd HH:mm:ss";
break;
case dtpCustomExtensions.dtpLongDateShortTime24Hour:
mvarCustomFormatMessage =
"Long Date Short Time 24 Hour (" +
DateTime.Now.ToString("D",
Application.CurrentCulture) + " " +
DateTime.Now.ToString("HH:mm",
Application.CurrentCulture) + ")";
this.CustomFormat = "yyyy-MM-dd HH:mm:ss";
break;
case dtpCustomExtensions.dtpLongDateShortTimeAMPM:
mvarCustomFormatMessage =
"Long Date Short Time AM/PM (" +
DateTime.Now.ToString("D",
Application.CurrentCulture) + " " +
DateTime.Now.ToString("hh:mm tt",
Application.CurrentCulture) + ")";
this.CustomFormat = "yyyy-MM-dd HH:mm:ss";
break;
case dtpCustomExtensions.dtpLongTime24Hour:
mvarCustomFormatMessage =
"Long Time 24 Hour (" +
DateTime.Now.ToString("HH:mm:ss",
Application.CurrentCulture) + ")";
this.CustomFormat = "HH:mm:ss yyyy-MM-dd ";
break;
case dtpCustomExtensions.dtpMonthFullName:
mvarCustomFormatMessage =
"Month Full Name (" +
DateTime.Now.ToString("MMMM",
Application.CurrentCulture) + ")";
this.CustomFormat = "MM-dd-yyyy HH:mm:ss";
break;
case dtpCustomExtensions.dtpMonthNameAndDay:
mvarCustomFormatMessage =
"Month Name and Day (" +
DateTime.Now.ToString("M",
Application.CurrentCulture) + ")";
this.CustomFormat = "dd-MM-yyyy HH:mm:ss";
break;
case dtpCustomExtensions.dtpMonthShortName:
mvarCustomFormatMessage =
"Month Short Name (" +
DateTime.Now.ToString("MMM",
Application.CurrentCulture) + ")";
this.CustomFormat = "MM-dd-yyyy HH:mm:ss";
this.CustomFormat = "yyyy-MM-dd HH:mm:ss";
break;
case dtpCustomExtensions.dtpShortDateLongTime24Hour:
mvarCustomFormatMessage =
"Short Date Long Time 24 Hour (" +
DateTime.Now.ToString("d",
Application.CurrentCulture) + " " +
DateTime.Now.ToString("HH:mm:ss",
Application.CurrentCulture) + ")";
this.CustomFormat = "yyyy-MM-dd HH:mm:ss";
break;
case dtpCustomExtensions.dtpShortDateLongTimeAMPM:
mvarCustomFormatMessage =
"Short Date Long Time AM/PM (" +
DateTime.Now.ToString("G",
Application.CurrentCulture) + ")";
this.CustomFormat = "yyyy-MM-dd HH:mm:ss";
break;
case dtpCustomExtensions.dtpShortDateShortTime24Hour:
mvarCustomFormatMessage =
" Short Date Short Time 24 Hour (" +
DateTime.Now.ToString("d",
Application.CurrentCulture) + " " +
DateTime.Now.ToString("HH:mm",
Application.CurrentCulture) + ")";
this.CustomFormat = "yyyy-MM-dd HH:mm:ss";
break;
case dtpCustomExtensions.dtpShortDateShortTimeAMPM:
mvarCustomFormatMessage =
" Short Date Short Time AM/PM (" +
DateTime.Now.ToString("d",
Application.CurrentCulture) + " " +
DateTime.Now.ToString("hh:mmss tt",
Application.CurrentCulture) + ")";
this.CustomFormat = "yyyy-MM-dd HH:mm:ss";
break;
case dtpCustomExtensions.dtpShortTime24Hour:
mvarCustomFormatMessage =
"Short Time 24 Hour (" +
DateTime.Now.ToString("HH:mm",
Application.CurrentCulture) + ")";
this.CustomFormat = "HH:mm:ss yyyy-MM-dd ";
break;
case dtpCustomExtensions.dtpShortTimeAMPM:
mvarCustomFormatMessage =
"Short Time AM/PM (" +
DateTime.Now.ToString("hh:mm tt",
Application.CurrentCulture) + ")";
this.CustomFormat = "HH:mm:ss yyyy-MM-dd ";
break;
case dtpCustomExtensions.dtpSortableDateAndTimeLocalTime:
mvarCustomFormatMessage =
"Sortable Date and Local Time (" +
DateTime.Now.ToString("s",
Application.CurrentCulture) + ")";
this.CustomFormat = "yyyy-MM-dd HH:mm:ss";
break;
case dtpCustomExtensions.dtpUTFLocalDateAndLongTime24Hour:
mvarCustomFormatMessage =
"UTF Local Date and Long Time 24 Hour (" +
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss",
Application.CurrentCulture) + ")";
this.CustomFormat = "yyyy-MM-dd HH:mm:ss";
break;
case dtpCustomExtensions.dtpUTFLocalDateAndLongTimeAMPM:
mvarCustomFormatMessage =
"UTF Local Date and Long Time AM/PM (" +
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss tt",
Application.CurrentCulture) + ")";
this.CustomFormat = "yyyy-MM-dd HH:mm:ss";
break;
case dtpCustomExtensions.dtpUTFLocalDateAndShortTime24Hour:
mvarCustomFormatMessage =
"UTF Local Date and Short Time 24 Hour (" +
DateTime.Now.ToString("yyyy-MM-dd HH:mm",
Application.CurrentCulture) + ")";
this.CustomFormat = "yyyy-MM-dd HH:mm:ss";
break;
case dtpCustomExtensions.dtpUTFLocalDateAndShortTimeAMPM:
mvarCustomFormatMessage =
"UTF Local Date and Short Time AM/PM (" +
DateTime.Now.ToString("yyyy-MM-dd HH:mm tt",
Application.CurrentCulture) + ")";
this.CustomFormat = "yyyy-MM-dd HH:mm:ss";
break;
case dtpCustomExtensions.dtpYear4Digit:
mvarCustomFormatMessage = "4 Digit Year (" +
DateTime.Now.ToString("yyyy",
Application.CurrentCulture);
this.CustomFormat = "yyyy-MM-dd HH:mm:ss";
break;
case dtpCustomExtensions.dtpYearAndMonthName:
mvarCustomFormatMessage =
"Year and Month Name (" +
DateTime.Now.ToString("Y",
Application.CurrentCulture) + ")";
this.CustomFormat = "MM-dd-yyyy HH:mm:ss";
break;
case dtpCustomExtensions.dtpShortDateAMPM:
mvarCustomFormatMessage = "Short Date AM/PM (" +
DateTime.Now.ToString("d",
Application.CurrentCulture) + " " +
DateTime.Now.ToString("tt",
Application.CurrentCulture) + ")";
this.CustomFormat = "MM-dd-yyyy HH:mm:ss";
break;
case dtpCustomExtensions.dtpShortDateMorningAfternoon:
string AMPM = "AM";
if (DateTime.Now.Hour >= 12)
{
AMPM = "Afternoon";
}
mvarCustomFormatMessage =
"Short Date Morning/Afternoon (" +
DateTime.Now.ToString("d",
Application.CurrentCulture) + " " +
AMPM + ")";
this.CustomFormat = "MM-dd-yyyy HH:mm:ss";
break;
}
Tooltip.SetToolTip(txtDateTime, mvarCustomFormatMessage);
}
//Dispplay dates Times etc, based on Format selected
private void FormatTextBox()
{
switch (mvarFormatEx)
{
case dtpCustomExtensions.dtpCustom:
this.Value = DateTime.Parse(txtDateTime.Text);
txtDateTime.Text =
this.Value.ToString(this.CustomFormat,
Application.CurrentCulture);
break;
case dtpCustomExtensions.dtpDayFullName:
try
{
this.Value = DateTime.Parse(txtDateTime.Text);
}
catch
{
int aDay;
DateTime aDate;
for (aDay = 1; aDay < 8; aDay++)
{
aDate = DateTime.Parse
(DateTime.Now.Year.ToString()
+ "-01-" + aDay.ToString());
if (aDate.DayOfWeek.ToString().ToLower() ==
txtDateTime.Text.ToLower() ||
aDate.DayOfWeek.ToString().
Substring(0, 3).ToLower()
== txtDateTime.Text.ToLower())
{
this.Value = DateTime.Parse
(DateTime.Now.Year.ToString() +
"-01-" + aDay.ToString());
break;
}
}
}
txtDateTime.Text = this.Value.ToString("dddd",
Application.CurrentCulture);
break;
case dtpCustomExtensions.dtpDayShortName:
try
{
this.Value = DateTime.Parse(txtDateTime.Text);
}
catch
{
int aDay;
DateTime aDate;
for (aDay = 1; aDay < 8; aDay++)
{
aDate = DateTime.Parse
(DateTime.Now.Year.ToString() +
"-01-" + aDay.ToString());
if (aDate.DayOfWeek.ToString().ToLower()
== txtDateTime.Text.ToLower() ||
aDate.DayOfWeek.ToString().Substring(0, 3).
ToLower() == txtDateTime.Text.ToLower())
{
this.Value = DateTime.Parse
(DateTime.Now.Year.ToString() +
"-01-" + aDay.ToString());
break;
}
}
}
txtDateTime.Text = this.Value.ToString("ddd",
Application.CurrentCulture);
break;
case dtpCustomExtensions.dtpLongDateLongTime24Hour:
this.Value = DateTime.Parse(txtDateTime.Text);
txtDateTime.Text = this.Value.ToString("D",
Application.CurrentCulture) + " " +
this.Value.ToString("HH:mm:ss",
Application.CurrentCulture);
break;
case dtpCustomExtensions.dtpLongDateLongTimeAMPM:
this.Value = DateTime.Parse(txtDateTime.Text);
txtDateTime.Text = this.Value.ToString("D",
Application.CurrentCulture) + " " +
this.Value.ToString("hh:mm:ss tt",
Application.CurrentCulture);
break;
case dtpCustomExtensions.dtpLongDateShortTime24Hour:
this.Value = DateTime.Parse(txtDateTime.Text);
txtDateTime.Text = this.Value.ToString("D",
Application.CurrentCulture) + " " +
this.Value.ToString("HH:mm",
Application.CurrentCulture);
break;
case dtpCustomExtensions.dtpLongDateShortTimeAMPM:
this.Value = DateTime.Parse(txtDateTime.Text);
txtDateTime.Text = this.Value.ToString("D",
Application.CurrentCulture) + " " +
this.Value.ToString("hh:mm tt",
Application.CurrentCulture);
break;
case dtpCustomExtensions.dtpLongTime24Hour:
this.Value = DateTime.Parse(txtDateTime.Text);
txtDateTime.Text = this.Value.ToString("HH:mm:ss",
Application.CurrentCulture);
break;
case dtpCustomExtensions.dtpMonthFullName:
try
{
this.Value = DateTime.Parse(txtDateTime.Text);
}
catch
{
int aMonth;
DateTime aDate;
string[] sMonth = new string[]{"Jan","Feb",
"Mar","Apr","May","Jun","Jul","Aug",
"Sep","Oct","Nov","Dec"};
for (aMonth = 0; aMonth < 12; aMonth++)
{
aDate = DateTime.Parse
(DateTime.Now.Year.ToString() +
"-" + (aMonth + 1) + "-" + "01");
if (sMonth[aMonth].ToLower() ==
txtDateTime.Text.ToLower() ||
sMonth[aMonth].ToLower() ==
txtDateTime.Text.Substring(0, 3).ToLower())
{
this.Value = DateTime.Parse
(DateTime.Now.Year.ToString()
+ "-"+ (aMonth + 1) + "-" + "01");
break;
}
}
}
txtDateTime.Text = this.Value.ToString("MMMM",
Application.CurrentCulture);
break;
case dtpCustomExtensions.dtpMonthShortName:
try
{
this.Value = DateTime.Parse(txtDateTime.Text);
}
catch
{
int aMonth;
DateTime aDate;
string[] sMonth = new string[]{"Jan",
"Feb","Mar","Apr","May","Jun","Jul",
"Aug","Sep","Oct","Nov","Dec"};
for (aMonth = 0; aMonth < 12; aMonth++)
{
aDate = DateTime.Parse
(DateTime.Now.Year.ToString() +
"-" + (aMonth + 1) + "-" + "01");
if (sMonth[aMonth].ToLower() ==
txtDateTime.Text.ToLower() ||
sMonth[aMonth].ToLower() ==
txtDateTime.Text.Substring(0, 3).ToLower())
{
this.Value = DateTime.Parse
(DateTime.Now.Year.ToString() +
"-"+ (aMonth + 1) + "-" + "01");
break;
}
}
}
txtDateTime.Text = this.Value.ToString("MMM",
Application.CurrentCulture);
break;
case dtpCustomExtensions.dtpMonthNameAndDay:
this.Value = DateTime.Parse(txtDateTime.Text);
txtDateTime.Text = this.Value.ToString("M",
Application.CurrentCulture);
break;
case dtpCustomExtensions.dtpShortDateLongTime24Hour:
this.Value = DateTime.Parse(txtDateTime.Text);
txtDateTime.Text = this.Value.ToString("d",
Application.CurrentCulture) + " " +
this.Value.ToString("HH:mms:ss",
Application.CurrentCulture);
break;
case dtpCustomExtensions.dtpShortDateLongTimeAMPM:
this.Value = DateTime.Parse(txtDateTime.Text);
txtDateTime.Text = this.Value.ToString("d",
Application.CurrentCulture) + " " +
this.Value.ToString("hh:mms:ss tt",
Application.CurrentCulture);
break;
case dtpCustomExtensions.dtpShortDateShortTime24Hour:
this.Value = DateTime.Parse(txtDateTime.Text);
txtDateTime.Text = this.Value.ToString("d",
Application.CurrentCulture) + " " +
this.Value.ToString("HH:mm",
Application.CurrentCulture);
break;
case dtpCustomExtensions.dtpShortDateShortTimeAMPM:
this.Value = DateTime.Parse(txtDateTime.Text);
txtDateTime.Text = this.Value.ToString("d",
Application.CurrentCulture) + " " +
this.Value.ToString("hh:mms tt",
Application.CurrentCulture);
break;
case dtpCustomExtensions.dtpShortTime24Hour:
this.Value = DateTime.Parse(txtDateTime.Text);
txtDateTime.Text = this.Value.ToString("HH:mm",
Application.CurrentCulture);
break;
case dtpCustomExtensions.dtpShortTimeAMPM:
this.Value = DateTime.Parse(txtDateTime.Text);
txtDateTime.Text = this.Value.ToString("hh:mm tt",
Application.CurrentCulture);
break;
case dtpCustomExtensions.dtpSortableDateAndTimeLocalTime:
this.Value = DateTime.Parse(txtDateTime.Text);
txtDateTime.Text = this.Value.ToString("s",
Application.CurrentCulture);
break;
case dtpCustomExtensions.dtpUTFLocalDateAndLongTime24Hour:
this.Value = DateTime.Parse(txtDateTime.Text);
txtDateTime.Text = this.Value.ToString("yyyy-MM-dd",
Application.CurrentCulture) + " " +
this.Value.ToString("HH:mm:ss",
Application.CurrentCulture);
break;
case dtpCustomExtensions.dtpUTFLocalDateAndLongTimeAMPM:
this.Value = DateTime.Parse(txtDateTime.Text);
txtDateTime.Text = this.Value.ToString("yyyy-MM-dd",
Application.CurrentCulture) + " " +
this.Value.ToString("hh:mm:ss tt",
Application.CurrentCulture);
break;
case dtpCustomExtensions.dtpUTFLocalDateAndShortTime24Hour:
this.Value = DateTime.Parse(txtDateTime.Text);
txtDateTime.Text = this.Value.ToString("yyyy-MM-dd",
Application.CurrentCulture) + " " +
this.Value.ToString("HH:mm",
Application.CurrentCulture);
break;
case dtpCustomExtensions.dtpUTFLocalDateAndShortTimeAMPM:
this.Value = DateTime.Parse(txtDateTime.Text);
txtDateTime.Text = this.Value.ToString("yyyy-MM-dd",
Application.CurrentCulture) + " " +
this.Value.ToString("hh:mm tt",
Application.CurrentCulture);
break;
case dtpCustomExtensions.dtpYear4Digit:
try
{
this.Value = DateTime.Parse(txtDateTime.Text);
}
catch
{
this.Value = DateTime.Parse("01 01 " +
txtDateTime.Text);
}
txtDateTime.Text = this.Value.ToString("yyyy",
Application.CurrentCulture);
break;
case dtpCustomExtensions.dtpYearAndMonthName:
try
{
this.Value = DateTime.Parse(txtDateTime.Text);
}
catch
{
try
{
txtDateTime.Text =
DateTime.Now.Year.ToString() + " " +
int.Parse(txtDateTime.Text,
Application.CurrentCulture).ToString();
}
catch
{
this.Value =
DateTime.Parse(txtDateTime.Text +
" 01" );
}
}
txtDateTime.Text = this.Value.ToString("Y",
Application.CurrentCulture);
break;
case dtpCustomExtensions.dtpShortDateAMPM:
if (txtDateTime.Text.Substring
(txtDateTime.Text.Length - 2, 2).ToLower()
== "pm")
{
txtDateTime.Text =
txtDateTime.Text.Substring(0,
txtDateTime.Text.Length - 2);
txtDateTime.Text = txtDateTime.Text +
" 13:00";
}
else
{
if (txtDateTime.Text.Substring
(txtDateTime.Text.Length - 2, 2).
ToLower() == "am")
{
txtDateTime.Text =
txtDateTime.Text.Substring(0,
txtDateTime.Text.Length - 2);
}
txtDateTime.Text = txtDateTime.Text + " 01:00";
}
this.Value = DateTime.Parse(txtDateTime.Text);
txtDateTime.Text = this.Value.ToString("d",
Application.CurrentCulture) + " " +
this.Value.ToString("tt",
Application.CurrentCulture);
break;
case dtpCustomExtensions.dtpShortDateMorningAfternoon:
string AMPM = "Morning";
if (txtDateTime.Text.Substring
(txtDateTime.Text.Length - 2, 2).
ToLower() == "pm")
{
txtDateTime.Text =
txtDateTime.Text.Substring(0,
txtDateTime.Text.Length - 2);
txtDateTime.Text = txtDateTime.Text + " 13:00";
}
else
{
if (txtDateTime.Text.Substring
(txtDateTime.Text.Length - 2, 2).
ToLower() == "am")
{
txtDateTime.Text =
txtDateTime.Text.Substring(0,
txtDateTime.Text.Length - 2);
}
txtDateTime.Text = txtDateTime.Text + " 01:00";
}
this.Value = DateTime.Parse(txtDateTime.Text);
if (this.Value.Hour >= 12)
{
AMPM = "Afternoon";
}
txtDateTime.Text = this.Value.ToString("d",
Application.CurrentCulture) + " " + AMPM;
break;
case dtpCustomExtensions.dtpLong:
this.Value = DateTime.Parse(txtDateTime.Text);
txtDateTime.Text = this.Value.ToLongDateString();
break;
case dtpCustomExtensions.dtpShort:
this.Value = DateTime.Parse(txtDateTime.Text);
txtDateTime.Text = this.Value.ToShortDateString();
break;
case dtpCustomExtensions.dtpTime:
this.Value = DateTime.Parse(txtDateTime.Text);
txtDateTime.Text = this.Value.ToLongTimeString();
break;
default:
break;
}
}
#endregion
}
}
关注点
就代码而言,它实际上只在讲英语的地区有用。虽然该控件会接受当前区域设置有效的任何格式的日期和/或时间,并根据 FormatEx
中选择的 Format
正确显示这些日期和时间,但所有消息都是英文的。