-
可以解决DateTimePicker 无法为DBNull的代码.已经测试通过,收藏
转自 http://www.codeguru.com/csharp/csharp/cs_controls/custom/article.php/c9645/
using System;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
// Copyright (c) 2005 Claudio Grazioli, http://www.grazioli.ch
//
// This implementation of a nullable DateTimePicker is a new implementation
// from scratch, but it is based on ideas I took from this nullable
// DateTimePickers:
// - http://www.omnitalented.com/Blog/PermaLink,guid,9ee757fe-a3e8-46f7-ad04-ef7070934dc8.aspx
// from Alexander Shirshov
// - http://www.codeproject.com/cs/miscctrl/Nullable_DateTimePicker.asp
// from Pham Minh Tri
//
// This code is free software; you can redistribute it and/or modify it.
// However, this header must remain intact and unchanged. Additional
// information may be appended after this header. Publications based on
// this code must also include an appropriate reference.
//
// This code is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.
//
namespace ProjectMentor.Windows.Controls
{
/**//// <summary>
/// Represents a Windows date time picker control. It enhances the .NET standard <b>DateTimePicker</b>
/// control with a ReadOnly mode as well as with the possibility to show empty values (null values).
/// </summary>
[ComVisible(false)]
public class NullableDateTimePicker : System.Windows.Forms.DateTimePicker
{
Member variables#region Member variables
// true, when no date shall be displayed (empty DateTimePicker)
private bool _isNull;
// If _isNull = true, this value is shown in the DTP
private string _nullValue;
// The format of the DateTimePicker control
private DateTimePickerFormat _format = DateTimePickerFormat.Long;
// The custom format of the DateTimePicker control
private string _customFormat;
// The format of the DateTimePicker control as string
private string _formatAsString;
#endregion
Constructor#region Constructor
/**//// <summary>
/// Default Constructor
/// </summary>
public NullableDateTimePicker() : base()
{
base.Format = DateTimePickerFormat.Custom;
NullValue = " ";
Format = DateTimePickerFormat.Long;
}
#endregion
Public properties#region Public properties
/**//// <summary>
/// Gets or sets the date/time value assigned to the control.
/// </summary>
/// <value>The DateTime value assigned to the control
/// </value>
/// <remarks>
/// <p>If the <b>Value</b> property has not been changed in code or by the user, it is set
/// to the current date and time (<see cref="DateTime.Now"/>).</p>
/// <p>If <b>Value</b> is <b>null</b>, the DateTimePicker shows
/// <see cref="NullValue"/>.</p>
/// </remarks>
public new Object Value
{
get
{
if (_isNull)
return null;
else
return base.Value;
}
set
{
if (value == null || value == DBNull.Value)
{
SetToNullValue();
}
else
{
SetToDateTimeValue();
base.Value = (DateTime)value;
}
}
}
/**//// <summary>
/// Gets or sets the format of the date and time displayed in the control.
/// </summary>
/// <value>One of the <see cref="DateTimePickerFormat"/> values. The default is
/// <see cref="DateTimePickerFormat.Long"/>.</value>
[Browsable(true)]
[DefaultValue(DateTimePickerFormat.Long), TypeConverter(typeof(Enum))]
public new DateTimePickerFormat Format
{
get { return _format; }
set
{
_format = value;
SetFormat();
OnFormatChanged(EventArgs.Empty);
}
}
/**//// <summary>
/// Gets or sets the custom date/time format string.
/// <value>A string that represents the custom date/time format. The default is a null
/// reference (<b>Nothing</b> in Visual Basic).</value>
/// </summary>
public new String CustomFormat
{
get { return _customFormat; }
set
{
_customFormat = value;
}
}
/**//// <summary>
/// Gets or sets the string value that is assigned to the control as null value.
/// </summary>
/// <value>The string value assigned to the control as null value.</value>
/// <remarks>
/// If the <see cref="Value"/> is <b>null</b>, <b>NullValue</b> is
/// shown in the <b>DateTimePicker</b> control.
/// </remarks>
[Browsable(true)]
[Category("Behavior")]
[Description("The string used to display null values in the control")]
[DefaultValue(" ")]
public String NullValue
{
get { return _nullValue; }
set { _nullValue = value; }
}
#endregion
Private methods/properties#region Private methods/properties
/**//// <summary>
/// Stores the current format of the DateTimePicker as string.
/// </summary>
private string FormatAsString
{
get { return _formatAsString; }
set
{
_formatAsString = value;
base.CustomFormat = value; }
}
/**//// <summary>
/// Sets the format according to the current DateTimePickerFormat.
/// </summary>
private void SetFormat()
{
CultureInfo ci = Thread.CurrentThread.CurrentCulture;
DateTimeFormatInfo dtf = ci.DateTimeFormat;
switch (_format)
{
case DateTimePickerFormat.Long:
FormatAsString = dtf.LongDatePattern;
break;
case DateTimePickerFormat.Short:
FormatAsString = dtf.ShortDatePattern;
break;
case DateTimePickerFormat.Time:
FormatAsString = dtf.ShortTimePattern;
break;
case DateTimePickerFormat.Custom:
FormatAsString = this.CustomFormat;
break;
}
}
/**//// <summary>
/// Sets the <b>DateTimePicker</b> to the value of the <see cref="NullValue"/> property.
/// </summary>
private void SetToNullValue()
{
_isNull = true;
base.CustomFormat = (_nullValue == null || _nullValue == String.Empty) ? " " : "'" + _nullValue + "'";
}
/**//// <summary>
/// Sets the <b>DateTimePicker</b> back to a non null value.
/// </summary>
private void SetToDateTimeValue()
{
if (_isNull)
{
SetFormat();
_isNull = false;
base.OnValueChanged(new EventArgs());
}
}
#endregion
OnXXXX()#region OnXXXX()
/**//// <summary>
/// This member overrides <see cref="DateTimePicker.OnCloseUp"/>.
/// </summary>
/// <param name="e"></param>
protected override void OnCloseUp(EventArgs e)
{
if (Control.MouseButtons == MouseButtons.None)
{
if (_isNull)
{
SetToDateTimeValue();
_isNull = false;
}
}
base.OnCloseUp (e);
}
/**//// <summary>
/// This member overrides <see cref="Control.OnKeyDown"/>.
/// </summary>
/// <param name="e"></param>
protected override void OnKeyUp(KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
this.Value = null;
OnValueChanged(EventArgs.Empty);
}
base.OnKeyUp(e);
}
#endregion
}
}
---------------------------------------------------------------------
每个人都是一座山.世上最难攀越的山,其实是自己.往上走,即便一小步,也有新高度.
--做最好的自己,我能!!!
-
相关阅读:
.Net中的加密解密
C#集合类
.NetFramework 1: I/O & Stream
系列4:文件操作以及读写文件
PetShop之表示层设计 :PetShop之表示层设计 (转)
const和static readonly 区别
ASP.NET经典源代码下载地址及数据库配置方法
一个知名出版商的挫折——解读 Wrox 的历史、现在与未来(转载)
《C++编程——数据结构与程序设计方法》程序范例:影碟店(源代码)
ASP.NET 英语词典
-
原文地址:https://www.cnblogs.com/tonyepaper/p/1203738.html
Copyright © 2020-2023
润新知