使用带有datetimepicker控件的Application.EnableVisualStyles()的错误 [英] bug using Application.EnableVisualStyles() with datetimepicker control

查看:80
本文介绍了使用带有datetimepicker控件的Application.EnableVisualStyles()的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将datetimepicker控件扩展为包含

a ReadOnly属性。

我使用new关键字来实现我自己的版本

of value属性,所以如果readonly == true那么

它将不会设置控件的值,并且当复选框的复选框的检查状态为false时将保留

用户

选择一个新的日期。


这在win2k机器上使用控件时工作正常

但如果我们使用它在一个win XP的盒子上然后调用

Application.EnableVisualStyles()然后它似乎忽略了我的代码
并勾选复选框并设置值。这是
极其错误的行为!还有价值属性

被调用两次(在win2k盒子上只有一次)。


i也注意到了
$ b中的行为变化使用XP视觉样式时$ b label.textalignment属性

还有。下面是我对扩展的

datetimepicker的实现,以及用于value属性的新数据类型

(OptionalDateTime)...


##### LockableDateTimePicker来源###########

使用系统;

使用System.Collections ;

使用System.ComponentModel;

使用System.Drawing;

使用System.Data;

使用System .Windows.Forms;

使用HCS.DataTypes;


名称空间HCS.Generic.UI.Controls

{

///< summary>

/// LockableDateTimePicker用于选择

日期。它操纵OptionalDateTime

///类以允许处理空日期。

///< / summary>

公共类LockableDateTimePicker:

DateTimePicker

{

#region清理代码


///<摘要>

///清理正在使用的所有资源。

///< / summary>

protected override void Dispose(bool

处理)

{

if(处理)

{

if(组件!= null)

{

components.Dispose

();

}

}

base.Dispose(disposing);

}


#endregion


#region组件设计器生成的代码

///< summary>

///设计师支持所需的方法 -

不要使用

代码编辑器修改

///此方法的内容。

///< / summary>

private void初始化组件()

{

components = new

System.ComponentModel.Container();

}

#endregion


#region字段


private System.ComponentModel.Container

组件= null;

private bool mReadOnly;

private DateTime mDateTime;

private Color mBackColor;

private Color mLockedColor ;

私人布尔mChecked;


#endregion


#region活动


公共活动OnReadOnlyChangedDelegate

OnReadOnlyChanged;


#endregion


#region EventArgs和Delegates


公共类OnReadOnlyChangedEventArgs:

EventArgs

{

private bool mReadOnly;


public OnReadOnlyChangedEventArgs

(bool ReadOnly)

{

mReadOnly = ReadOnly;

}

public bool ReadOnly

{

get

{

返回mReadOnly;

}

}

}

public delegate void

OnReadOnlyChangedDelegate(对象发件人,

OnReadOnlyChangedEventArgs e);


#endregion

#region构造函数


public LockableDateTimePicker()

{

// <需要此调用

Windows.Forms表单设计器。

InitializeComponent();


mBackColor =

base.CalendarMonthBackground ;

mLockedColor =

base.CalendarMonthBackground;


//设置此控件的默认值

base.Format =

DateTimePickerFormat.Short;


//确保我们的日期备份

已填充

mDateTime = base.Value;

mChecked = base.Checked;

}


#endregion


#region属性


[DesignerSerializationVisibility

(DesignerSer) ializationVisibility.Visible)]

public bool ReadOnly

{

get

{

返回mReadOnly;

}

set

{

if(value)

{


base.CalendarMonthBackground = mLockedColor;

}

else

{


base.CalendarMonthBackground = mBackColor;

}


mReadOnly = value;

if(b) OnReadOnlyChanged!=

null)

{

OnReadOnlyChanged

(这是新的OnReadOnlyChangedEventArgs(值)); < br $>
}

}

}


[DesignerSerializationVisibility

(DesignerSerializationVisibility) .Visible)]

公共Color LockedColor

{

get

{

返回mLockedColor;

}

set

{

mLockedColor = value;

}

}


#endregion


#region Public Overridde n属性

公共覆盖颜色BackColor

{

get

{

return base.BackColor;

}

set

{

mBackColor = value;

if(!mReadOnly)

{

base.BackColor =

value;

}

}

}

public new OptionalDateTime Value

{

set

{

if(value.GetValue()=="")

{

base.Checked =

false;

}

else

{

base.Value =

DateTime.Parse(value.GetValue());

}


}

get

{

if(base.Checked)

{

return new

OptionalDateTime(base.Value );

}

其他

{

返回新

OptionalDateTime() ;

}

}

}

#endregion


#region Public Overridden Events


protected override void OnValueChanged

(EventArgs eventargs)

{

base.OnValueChanged(eventargs );


if(mReadOnly)

{

//我们需要设置

控制值返回

//存储值,因为

它是只读的

if(base.Value!=

mDateTime)

{

base.Value =

mDateTime;

}

if(base.Checked!=

mChecked)

{

base.Checked =

mChecked;

}

}

else

{

//当它是只读的时候存储

的值

mDateTime = base.Value;

mChecked = base.Checked;

}

}


#endregion


#region公共方法

public void Initialise(OptionalDateTime

Value)

{

//暂时将控件设置为

不是ReadOn如果(mReadOnly)

{

mReadOnly = false;

}


if(Value.GetValue()=="")

{

base.Checked = false;

mChecked = false;

}

else

{

base.Value =

DateTime.Parse(Value.GetValue());

base.Checked = true;

mDateTime = base.Value;

mChecked = true;

}


//确保ReadOnly值为

恢复正常

mReadOnly = mTempReadOnly;

}

public void Initialise(DateTime Value)

{

初始化(新的OptionalDateTime

(价值));

}

public void Initialise()

{

初始化(新的OptionalDateTime

());

}

#endregion

}

}


############## OptionalDateTime Source #### ######


[Serializable()]

public class OptionalDateTime

{

#region Enum

///< ;摘要>

///可用格式 - 需要延伸为

,但请记得更新< see cref =" GetValue" /> ;.

///< / summary>

public enum enumDateTimeFormat

{

///< summary>

/// LongDateFormat

///< / summary>

LongDateFormat,

///< summary>

/// LongTimeFormat

///< / summary>

LongTimeFormat,

///< ;摘要>

/// ShortDateFormat

///< / summary>

ShortDateFormat,

/ //< summary>

/// ShortTimeFormat

///< / summary>

ShortTimeFormat

}

#endregion


#region字段

private DateTime mDate;

private bool mIsNull ;

#endregion


#region构造函数

///< summary>

///构造函数 - 初始化null

OptionalDateTime

///< / summary>

public OptionalDateTime()

{

mIsNull = true;

}

///< summary>

///构造函数 - 初始化

OptionalDateTime包含字符串的值。

///如果字符串不是有效的

DateTime,则该对象设置为包含空日期。

/// < / summary>

///< param name =" value">一个字符串

代表一个有效的日期。< / param>

public OptionalDateTime(字符串值)

{

SetValue(value);

}

///< summary>

///构造函数 - 初始化

OptionalDateTime以包含DateTime的值。

/// < / summary>

///< param name =" value">日期时间值

类型。< ; / param>

public OptionalDateTime(DateTime value)

{

SetValue(value);

}

#endregion


#region公共方法

///< summary>

// /设置对象的值等于

DateTime的值。

///< / summary>

///< ; param name =" value"> A

DateTime。< / param>

public void SetValue(DateTime value)

{

mDate = value;

mIsNull = false;

}

///< summary>

///将对象的值设置为等于

字符串的值。如果该字符串不是有效的

/// DateTime,则该对象设置为

包含空日期。

///< ; / summary>

///< param name =" value">一个字符串

代表一个有效的日期。< / param>

public void SetValue(string value)

{

if(value == null || value =="")

{

mIsNull = true;

}

else

{

尝试

{

mDate =

DateTime.Parse(value);

mIsNull = false;

}

catch

{

抛出新的

ArgumentException("输入的字符串不能转换

到日期时间,价值);

}

}

}

///< summary>

///将对象的值作为带有可选格式的

字符串返回。

///< / summary>

///< param name =" Format"> The fo rmat to

return。< / param>

///< returns>包含

正确格式化日期的字符串。< / return>

公共字符串GetValue(enumDateTimeFormat

格式)

{

if(mIsNull)

{

返回"" ;;

}

其他

{

开关(格式)

{

案例

enumDateTimeFormat.LongDateFormat:

返回

mDate.ToLongDateString();

case

enumDateTimeFormat.LongTimeFormat:

return

mDate.ToLongTimeString();

case

enumDateTimeFormat.ShortDateFormat:

return

mDate.ToShortDateString();

案例

enumDateTimeFormat.ShortTimeFormat:

返回

mDate.ToShortTimeString();

默认:

抛出新的

UnhandledDateFormatException(格式);

}

}

}

///&l t;摘要>

///将对象的值返回为

ShortDateString。

///< / summary>

///< returns>< / returns>

public string GetValue()

{

return GetValue

(enumDateTimeFormat.ShortDateFormat);

}

#endregion


#region Public Override方法

///< summary>

///在< see cref =" enumDateTimeFormat中传递包含日期

的字符串.ShortDateFormat" /> ;.

///< / summary>

///< returns>返回的日期。为空(null)日期传递

空字符串。< / returns>

公共覆盖字符串ToString()

{

返回GetValue();

}

#endregion


#region属性

///< summary>

///返回

OptionalDateTime作为DateTime类型的日期。提高

///<参见

cref =" OptionalDateTimeIsNullException" />如果

OptionalDateTime是

/// blank(null)。检查<请参阅

cref =" IsNull" />调用此属性之前的属性

///避免异常的方法。

///< / summary>

public DateTime GetDateTime

{

get

{

if(mIsNull)

{

抛出新的

OptionalDateTimeIsNullException();

}

返回mDate;

}

}

///< summary>

///获取一个布尔值,表示

OptionalDateTime是否为空(null)。

///< / summary>

public bool IsNull

{

get

{

返回mIsNull;

}

}

#endregion

}


提前感谢

I have extended the datetimepicker control to incorporate
a ReadOnly property.
I have used the new keyword to implement my own version
of the value property, so that if readonly == true then
it will not set the value of the control and will leave
the checked status of the checkbox to false when a user
selects a new date.

this works fine when using the control on a win2k machine
but if we use it on a win XP box and call
Application.EnableVisualStyles() then it seems to ignore
my code and check the checkbox and set the value. this is
extremely buggy behaviour! and also the value property
gets called twice (only once on a win2k box).

i have also noticed behaviour changes in the
label.textalignment property when using XP Visual styles
aswell. Below is my implementation of the extended
datetimepicker and also the new data type
(OptionalDateTime) that is used for the value property...

#####LockableDateTimePicker Source###########

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using HCS.DataTypes;

namespace HCS.Generic.UI.Controls
{
/// <summary>
/// LockableDateTimePicker is for selecting
dates. It manipulates the OptionalDateTime
/// class to allow null dates to be handled.
/// </summary>
public class LockableDateTimePicker :
DateTimePicker
{
#region Clean Up Code

/// <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

#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()
{
components = new
System.ComponentModel.Container();
}
#endregion

#region Fields

private System.ComponentModel.Container
components = null;
private bool mReadOnly;
private DateTime mDateTime;
private Color mBackColor;
private Color mLockedColor;
private bool mChecked;

#endregion

#region Events

public event OnReadOnlyChangedDelegate
OnReadOnlyChanged;

#endregion

#region EventArgs and Delegates

public class OnReadOnlyChangedEventArgs :
EventArgs
{
private bool mReadOnly;

public OnReadOnlyChangedEventArgs
(bool ReadOnly)
{
mReadOnly = ReadOnly;
}
public bool ReadOnly
{
get
{
return mReadOnly;
}
}
}
public delegate void
OnReadOnlyChangedDelegate(object sender,
OnReadOnlyChangedEventArgs e);

#endregion

#region Constructor

public LockableDateTimePicker()
{
// This call is required by the
Windows.Forms Form Designer.
InitializeComponent();

mBackColor =
base.CalendarMonthBackground;
mLockedColor =
base.CalendarMonthBackground;

//Set defaults for this control
base.Format =
DateTimePickerFormat.Short;

//Make sure that our date backup
is populated
mDateTime = base.Value;
mChecked = base.Checked;
}

#endregion

#region Properties

[DesignerSerializationVisibility
(DesignerSerializationVisibility.Visible)]
public bool ReadOnly
{
get
{
return mReadOnly;
}
set
{
if(value)
{

base.CalendarMonthBackground = mLockedColor;
}
else
{

base.CalendarMonthBackground = mBackColor;
}

mReadOnly = value;
if(OnReadOnlyChanged !=
null)
{
OnReadOnlyChanged
(this, new OnReadOnlyChangedEventArgs(value));
}
}
}

[DesignerSerializationVisibility
(DesignerSerializationVisibility.Visible)]
public Color LockedColor
{
get
{
return mLockedColor;
}
set
{
mLockedColor = value;
}
}

#endregion

#region Public Overridden Properties
public override Color BackColor
{
get
{
return base.BackColor;
}
set
{
mBackColor = value;
if(!mReadOnly)
{
base.BackColor =
value;
}
}
}
public new OptionalDateTime Value
{
set
{
if(value.GetValue() == "")
{
base.Checked =
false;
}
else
{
base.Value =
DateTime.Parse(value.GetValue());
}

}
get
{
if(base.Checked)
{
return new
OptionalDateTime(base.Value);
}
else
{
return new
OptionalDateTime();
}
}
}
#endregion

#region Public Overridden Events

protected override void OnValueChanged
(EventArgs eventargs)
{
base.OnValueChanged (eventargs);

if(mReadOnly)
{
//We need to set the
value of the control back to
//the stored value, since
it is read only
if(base.Value !=
mDateTime)
{
base.Value =
mDateTime;
}
if(base.Checked !=
mChecked)
{
base.Checked =
mChecked;
}
}
else
{
//Store the value for
when it''s read only
mDateTime = base.Value;
mChecked = base.Checked;
}
}

#endregion

#region Public Methods
public void Initialise(OptionalDateTime
Value)
{
//Temporarily set the control to
not ReadOnly.
bool mTempReadOnly = mReadOnly;
if(mReadOnly)
{
mReadOnly = false;
}

if(Value.GetValue() == "")
{
base.Checked = false;
mChecked = false;
}
else
{
base.Value =
DateTime.Parse(Value.GetValue());
base.Checked = true;
mDateTime = base.Value;
mChecked = true;
}

//Make sure the ReadOnly value is
returned to normal
mReadOnly = mTempReadOnly;
}
public void Initialise(DateTime Value)
{
Initialise(new OptionalDateTime
(Value));
}
public void Initialise()
{
Initialise(new OptionalDateTime
());
}
#endregion
}
}

##############OptionalDateTime Source##########

[Serializable()]
public class OptionalDateTime
{
#region Enum
/// <summary>
/// Formats available - extend as
required, but remember to update <see cref="GetValue"/>.
/// </summary>
public enum enumDateTimeFormat
{
/// <summary>
/// LongDateFormat
/// </summary>
LongDateFormat,
/// <summary>
/// LongTimeFormat
/// </summary>
LongTimeFormat,
/// <summary>
/// ShortDateFormat
/// </summary>
ShortDateFormat,
/// <summary>
/// ShortTimeFormat
/// </summary>
ShortTimeFormat
}
#endregion

#region Fields
private DateTime mDate;
private bool mIsNull;
#endregion

#region Constructor
/// <summary>
/// Constructor - initialises a null
OptionalDateTime
/// </summary>
public OptionalDateTime()
{
mIsNull = true;
}
/// <summary>
/// Constructor - initialise an
OptionalDateTime to contain the value of a string.
/// If the string is not a valid
DateTime, the object is set to contain a null date.
/// </summary>
/// <param name="value">A string
representing a valid date.</param>
public OptionalDateTime(string value)
{
SetValue(value);
}
/// <summary>
/// Constructor - initialise an
OptionalDateTime to contain the value of a DateTime.
/// </summary>
/// <param name="value">A DateTime value
type.</param>
public OptionalDateTime(DateTime value)
{
SetValue(value);
}
#endregion

#region Public Methods
/// <summary>
/// Set the value of the object to equal
that of a DateTime.
/// </summary>
/// <param name="value">A
DateTime.</param>
public void SetValue(DateTime value)
{
mDate = value;
mIsNull = false;
}
/// <summary>
/// Set the value of the object to equal
that of a string. If the string is not a valid
/// DateTime, the object is set to
contain a null date.
/// </summary>
/// <param name="value">A string
representing a valid date.</param>
public void SetValue(string value)
{
if(value == null || value == "")
{
mIsNull = true;
}
else
{
try
{
mDate =
DateTime.Parse(value);
mIsNull = false;
}
catch
{
throw new
ArgumentException("The string entered cannot be converted
to a DateTime", "value");
}
}
}
/// <summary>
/// Return the value of the object as a
string with optional formatting.
/// </summary>
/// <param name="Format">The format to
return.</param>
/// <returns>A string containing the
correctly formatted date.</returns>
public string GetValue(enumDateTimeFormat
Format)
{
if(mIsNull)
{
return "";
}
else
{
switch(Format)
{
case
enumDateTimeFormat.LongDateFormat:
return
mDate.ToLongDateString();
case
enumDateTimeFormat.LongTimeFormat:
return
mDate.ToLongTimeString();
case
enumDateTimeFormat.ShortDateFormat:
return
mDate.ToShortDateString();
case
enumDateTimeFormat.ShortTimeFormat:
return
mDate.ToShortTimeString();
default:
throw new
UnhandledDateFormatException(Format);
}
}
}
/// <summary>
/// Return the value of the object as a
ShortDateString.
/// </summary>
/// <returns></returns>
public string GetValue()
{
return GetValue
(enumDateTimeFormat.ShortDateFormat);
}
#endregion

#region Public Override Methods
/// <summary>
/// Passes a string containing the date
in <see cref="enumDateTimeFormat.ShortDateFormat"/>.
/// </summary>
/// <returns>The date returned. Passes an
empty string for blank (null) dates.</returns>
public override string ToString()
{
return GetValue();
}
#endregion

#region Properties
/// <summary>
/// Returns the date of the
OptionalDateTime as a DateTime type. Raises the
/// <see
cref="OptionalDateTimeIsNullException"/> if the
OptionalDateTime is
/// blank (null). Check the <see
cref="IsNull"/> property before calling this
/// method to avoid the exception.
/// </summary>
public DateTime GetDateTime
{
get
{
if(mIsNull)
{
throw new
OptionalDateTimeIsNullException();
}
return mDate;
}
}
/// <summary>
/// Gets a boolean value indicating
whether the OptionalDateTime is blank (null).
/// </summary>
public bool IsNull
{
get
{
return mIsNull;
}
}
#endregion
}

thanks in advance

推荐答案



盖伊,


这是一个众所周知的问题。

我会对此做一些研究,为你找到一些解决方法。

感谢您的理解。


祝你好运,

Jeffrey Tan

Microsoft在线合作伙伴支持

安全! - www.microsoft.com/security

此帖子按原样提供没有保证,也没有赋予任何权利。


--------------------

| Content-Class:urn:content-classes:message

|来自:盖伊 < gu*@hcs-ltd.co.uk>

|发件人:盖伊 < gu*@hcs-ltd.co.uk>

|主题:使用带有datetimepicker的Application.EnableVisualStyles()的bug

控制

|日期:2003年10月24日星期五03:56:32 -0700

|行数:540

|消息ID:< 08 **************************** @ phx.gbl>

| MIME版本:1.0

|内容类型:text / plain;

| charset =" iso-8859-1"

|内容传输编码:7位

| X-Newsreader:适用于Windows 2000的Microsoft CDO

| Thread-Index:AcOaHXwtoiMaL / QOSWuDQUgjMGLnAw ==

| X-MimeOLE:由Microsoft MimeOLE制作V5.50.4910.0300

|新闻组:microsoft.public.dotnet.languages.csharp

|路径:cpmsftngxa06.phx.gbl

|外翻:cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:193774

| NNTP-Posting-Host:TK2MSFTNGXA13 10.40.1.165

| X-Tomcat-NG:microsoft.public.dotnet.languages.csharp

|

|我已将datetimepicker控件扩展为包含

| ReadOnly属性。

|我使用new关键字来实现我自己的版本

| value属性的,如果readonly == true则

|它不会设置控件的值,并将离开

|当用户

|时,复选框的选中状态为false选择一个新的日期。

|

|这在win2k机器上使用控件时工作正常

|但是如果我们在win XP盒子上使用它并打电话给

| Application.EnableVisualStyles()然后似乎忽略了

|我的代码并选中复选框并设置值。这是

|极其错误的行为!还有价值物业

|被叫两次(在win2k盒子上只有一次)。

|

|我也注意到了

|中的行为变化使用XP视觉样式时的label.textalignment属性

|以及。下面是我对扩展

|的实现datetimepicker以及新数据类型

| (OptionalDateTime)用于value属性...

|

| ##### LockableDateTimePicker来源###########

|

|使用系统;

|使用System.Collections;

|使用System.ComponentModel;

|使用System.Drawing;

|使用System.Data;

|使用System.Windows.Forms;

|使用HCS.DataTypes;

|

| namespace HCS.Generic.UI.Controls

| {

| ///< summary>

| /// LockableDateTimePicker用于选择

|日期。它操纵OptionalDateTime

| ///类允许处理空日期。

| ///< / summary>

|公共类LockableDateTimePicker:

| DateTimePicker

| {

| #region清理代码

|

| ///< summary>

| ///清理正在使用的所有资源。

| ///< / summary>

| protected override void Dispose(bool

| disposing)

| {

| if(处理)

| {

| if(components!= null)

| {

| components.Dispose

| ();

| }

| }

| base.Dispose(disposing);

| }

|

| #endregion

|

| #region组件设计器生成的代码

| ///< summary>

| ///支持Designer的必需方法 -

|不要修改

| ///这个方法的内容用

|代码编辑器。

| ///< / summary>

| private void InitializeComponent()

| {

| components = new

| System.ComponentModel.Container();

| }

| #endregion

|

| #region Fields

|

| private System.ComponentModel.Container

| components = null;

| private bool mReadOnly;

| private DateTime mDateTime;

|私人颜色mBackColor;

|私人颜色mLockedColor;

|私人bool mChecked;

|

| #endregion

|

| #region活动

|

|公共事件OnReadOnlyChangedDelegate

| OnReadOnlyChanged;

|

| #endregion

|

| #region EventArgs和代表

|

|公共类OnReadOnlyChangedEventArgs:

| EventArgs

| {

| private bool mReadOnly;

|

| public OnReadOnlyChangedEventArgs

| (bool ReadOnly)

| {

| mReadOnly = ReadOnly;

| }

| public bool ReadOnly

| {

|得到

| {

|返回mReadOnly;

| }

| }

| }

|公众代表无效

| OnReadOnlyChangedDelegate(对象发件人,

| OnReadOnlyChangedEventArgs e);

|

| #endregion

|

| #region构造函数

|

| public LockableDateTimePicker()

| {

| //

|需要这个电话Windows.Forms表单设计器。

| InitializeComponent();

|

| mBackColor =

| base.CalendarMonthBackground;

| mLockedColor =

| base.CalendarMonthBackground;

|

| //设置此控件的默认值

| base.Format =

| DateTimePickerFormat.Short;

|

| //确保我们的日期备份

|填充

| mDateTime = base.Value;

| mChecked = base.Checked;

| }

|

| #endregion

|

| #region Properties

|

| [DesignerSerializationVisibility

| (DesignerSerializationVisibility.Visible)]

| public bool ReadOnly

| {

|得到

| {

|返回mReadOnly;

| }

|设置

| {

| if(value)

| {

|

| base.CalendarMonthBackground = mLockedColor;

| }

|否则

| {

|

| base.CalendarMonthBackground = mBackColor;

| }

|

| mReadOnly = value;

| if(OnReadOnlyChanged!=

| null)

| {

| OnReadOnlyChanged

| (这个,新的OnReadOnlyChangedEventArgs(值));

| }

| }

| }

|

| [DesignerSerializationVisibility

| (DesignerSerializationVisibility.Visible)]

| public Color LockedColor

| {

|得到

| {

|返回mLockedColor;

| }

|设置

| {

| mLockedColor = value;

| }

| }

|

| #endregion

|

| #region Public Overridden Properties

|公共覆盖Color BackColor

| {

|得到

| {

| return base.BackColor;

| }

|设置

| {

| mBackColor = value;

| if(!mReadOnly)

| {

| base.BackColor =

|价值;

| }

| }

| }

| public new OptionalDateTime值

| {

|设置

| {

| if(value.GetValue()=="")

| {

| base.Checked =

|假;

| }

|否则

| {

| base.Value =

| DateTime.Parse(value.GetValue());

| }

|

| }

|得到

| {

| if(base.Checked)

| {

|返回新的

| OptionalDateTime(base.Value);

| }

|否则

| {

|返回新的

| OptionalDateTime();

| }

| }

| }

| #endregion

|

| #region Public Overridden Events

|

| protected override void OnValueChanged

| (EventArgs eventargs)

| {

| base.OnValueChanged(eventargs);

|

| if(mReadOnly)

| {

| //我们需要设置

|控制值回到

| //存储的值,因为

|它是只读的

| if(base.Value!=

| mDateTime)

| {

| base.Value =

| mDateTime;

| }

| if(base.Checked!=

| mChecked)

| {

| base.Checked =

| mChecked;

| }

| }

|否则

| {

| //存储

|的值当它是只读的时候

| mDateTime = base.Value;

| mChecked = base.Checked;

| }

| }

|

| #endregion

|

| #region公共方法

| public void Initialise(OptionalDateTime

| Value)

| {

| //暂时将控件设置为

|不是ReadOnly。

| bool mTempReadOnly = mReadOnly;

| if(mReadOnly)

| {

| mReadOnly = false;

| }

|

| if(Value.GetValue()=="")

| {

| base.Checked = false;

| mChecked = false;

| }

|否则

| {

| base.Value =

| DateTime.Parse(Value.GetValue());

| base.Checked = true;

| mDateTime = base.Value;

| mChecked = true;

| }

|

| //确保ReadOnly值为

|恢复正常

| mReadOnly = mTempReadOnly;

| }

| public void Initialise(DateTime Value)

| {

|初始化(new OptionalDateTime

|(Value));

| }

| public void Initialise()

| {

|初始化(new OptionalDateTime

|());

| }

| #endregion

| }

| }

|

| ############## OptionalDateTime Source ##########

|

| [Serializable()]

| public class OptionalDateTime

| {

| #region Enum

| ///< summary>

| ///格式可用 - 扩展为

|必填,但请记得更新< see cref =" GetValue" /> ;.

| ///< / summary>

| public enum enumDateTimeFormat

| {

| ///< summary>

| /// LongDateFormat

| ///< / summary>

| LongDateFormat,

| ///< summary>

| /// LongTimeFormat

| ///< / summary>

| LongTimeFormat,

| ///< summary>

| /// ShortDateFormat

| ///< / summary>

| ShortDateFormat,

| ///< summary>

| /// ShortTimeFormat

| ///< / summary>

| ShortTimeFormat

| }

| #endregion

|

| #region Fields

| private DateTime mDate;

|私人bool mIsNull;

| #endregion

|

| #region构造函数

| ///< summary>

| ///构造函数 - 初始化null

| OptionalDateTime

| ///< / summary>

| public OptionalDateTime()

| {

| mIsNull = true;

| }

| ///< summary>

| ///构造函数 - 初始化

| OptionalDateTime包含字符串的值。

| ///如果字符串不是有效的

| DateTime,该对象设置为包含空日期。

| ///< / summary>

| ///< param name =" value">一个字符串

|表示有效日期。< / param>

| public OptionalDateTime(字符串值)

| {

| SetValue(value);

| }

| ///< summary>

| ///构造函数 - 初始化

| OptionalDateTime包含DateTime的值。

| ///< / summary>

| ///< param name =" value">日期时间值

|类型。< / param>

| public OptionalDateTime(DateTime value)

| {

| SetValue(value);

| }

| #endregion

|

| #region公共方法

| ///< summary>

| ///将对象的值设置为等于

|日期时间。

| ///< / summary>

| ///< param name =" value"> A

| DateTime。< / param>

| public void SetValue(DateTime value)

| {

| mDate = value;

| mIsNull = false;

| }

| ///< summary>

| ///将对象的值设置为等于

|一个字符串。如果字符串不是有效的

| /// DateTime,对象设置为

|包含一个空日期。

| ///< / summary>

| ///< param name =" value">一个字符串

|表示有效日期。< / param>

| public void SetValue(string value)

| {

| if(value == null || value =="")

| {

| mIsNull = true;

| }

|否则

| {

|试试

| {

| mDate =

| DateTime.Parse(value);

| mIsNull = false;

| }

|赶上

| {

|抛出新的

| ArgumentException("输入的字符串无法转换

|到DateTime"," value");

| }

| }

| }

| ///< summary>

| ///将对象的值返回为

|带有可选格式的字符串。

| ///< / summary>

| ///< param name =" Format">格式为

|返回。< / param>

| ///< returns>包含

|的字符串格式正确的日期。< / returns>

| public string GetValue(enumDateTimeFormat

|格式)

| {

| if(mIsNull)

| {

|返回" ;;

| }

|否则

| {

|开关(格式)

| {

|案件

| enumDateTimeFormat.LongDateFormat:

|返回

| mDate.ToLongDateString();

|案件

| enumDateTimeFormat.LongTimeFormat:

|返回

| mDate.ToLongTimeString();

|案件

| enumDateTimeFormat.ShortDateFormat:

|返回

| mDate.ToShortDateString();

|案件

| enumDateTimeFormat.ShortTimeFormat:

|返回

| mDate.ToShortTimeString();

|默认值:

|抛出新的

| UnhandledDateFormatException(格式);

| }

| }

| }

| ///< summary>

| ///将对象的值返回为

| ShortDateString。

| ///< / summary>

| ///< returns>< / returns>

| public string GetValue()

| {

|返回GetValue

| (enumDateTimeFormat.ShortDateFormat);

| }

| #endregion

|

| #region公共覆盖方法

| ///< summary>

| ///传递一个包含日期

|的字符串in< see cref =" enumDateTimeFormat.ShortDateFormat" /> ;.

| ///< / summary>

| ///< returns>返回的日期。通过

|空字符串为空(null)日期。< / returns>

| public override string ToString()

| {

|返回GetValue();

| }

| #endregion

|

| #region Properties

| ///< summary>

| ///返回

|的日期OptionalDateTime作为DateTime类型。提高

| ///<见

| CREF = QUOT; OptionalDateTimeIsNullException" />如果

| OptionalDateTime是

| /// blank(null)。检查<看

| CREF = QUOT; ISNULL" />在致电之前的财产

| ///避免异常的方法。

| ///< / summary>

| public DateTime GetDateTime

| {

|得到

| {

| if(mIsNull)

| {

|抛出新的

| OptionalDateTimeIsNullException();

| }

|返回mDate;

| }

| }

| ///< summary>

| ///获取一个布尔值,表示

| OptionalDateTime是否为空(null)。

| ///< / summary>

| public bool IsNull

| {

|得到

| {

| return mIsNull;

| }

| }

| #endregion

| }

|

|提前谢谢

|


Hi Guy,

It is an already known issue.
I will do some research on this to find some workaround for you.
Thanks for you understanding.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Content-Class: urn:content-classes:message
| From: "Guy" <gu*@hcs-ltd.co.uk>
| Sender: "Guy" <gu*@hcs-ltd.co.uk>
| Subject: bug using Application.EnableVisualStyles() with datetimepicker
control
| Date: Fri, 24 Oct 2003 03:56:32 -0700
| Lines: 540
| Message-ID: <08****************************@phx.gbl>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Thread-Index: AcOaHXwtoiMaL/QOSWuDQUgjMGLnAw==
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:193774
| NNTP-Posting-Host: TK2MSFTNGXA13 10.40.1.165
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| I have extended the datetimepicker control to incorporate
| a ReadOnly property.
| I have used the new keyword to implement my own version
| of the value property, so that if readonly == true then
| it will not set the value of the control and will leave
| the checked status of the checkbox to false when a user
| selects a new date.
|
| this works fine when using the control on a win2k machine
| but if we use it on a win XP box and call
| Application.EnableVisualStyles() then it seems to ignore
| my code and check the checkbox and set the value. this is
| extremely buggy behaviour! and also the value property
| gets called twice (only once on a win2k box).
|
| i have also noticed behaviour changes in the
| label.textalignment property when using XP Visual styles
| aswell. Below is my implementation of the extended
| datetimepicker and also the new data type
| (OptionalDateTime) that is used for the value property...
|
| #####LockableDateTimePicker Source###########
|
| using System;
| using System.Collections;
| using System.ComponentModel;
| using System.Drawing;
| using System.Data;
| using System.Windows.Forms;
| using HCS.DataTypes;
|
| namespace HCS.Generic.UI.Controls
| {
| /// <summary>
| /// LockableDateTimePicker is for selecting
| dates. It manipulates the OptionalDateTime
| /// class to allow null dates to be handled.
| /// </summary>
| public class LockableDateTimePicker :
| DateTimePicker
| {
| #region Clean Up Code
|
| /// <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
|
| #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()
| {
| components = new
| System.ComponentModel.Container();
| }
| #endregion
|
| #region Fields
|
| private System.ComponentModel.Container
| components = null;
| private bool mReadOnly;
| private DateTime mDateTime;
| private Color mBackColor;
| private Color mLockedColor;
| private bool mChecked;
|
| #endregion
|
| #region Events
|
| public event OnReadOnlyChangedDelegate
| OnReadOnlyChanged;
|
| #endregion
|
| #region EventArgs and Delegates
|
| public class OnReadOnlyChangedEventArgs :
| EventArgs
| {
| private bool mReadOnly;
|
| public OnReadOnlyChangedEventArgs
| (bool ReadOnly)
| {
| mReadOnly = ReadOnly;
| }
| public bool ReadOnly
| {
| get
| {
| return mReadOnly;
| }
| }
| }
| public delegate void
| OnReadOnlyChangedDelegate(object sender,
| OnReadOnlyChangedEventArgs e);
|
| #endregion
|
| #region Constructor
|
| public LockableDateTimePicker()
| {
| // This call is required by the
| Windows.Forms Form Designer.
| InitializeComponent();
|
| mBackColor =
| base.CalendarMonthBackground;
| mLockedColor =
| base.CalendarMonthBackground;
|
| //Set defaults for this control
| base.Format =
| DateTimePickerFormat.Short;
|
| //Make sure that our date backup
| is populated
| mDateTime = base.Value;
| mChecked = base.Checked;
| }
|
| #endregion
|
| #region Properties
|
| [DesignerSerializationVisibility
| (DesignerSerializationVisibility.Visible)]
| public bool ReadOnly
| {
| get
| {
| return mReadOnly;
| }
| set
| {
| if(value)
| {
|
| base.CalendarMonthBackground = mLockedColor;
| }
| else
| {
|
| base.CalendarMonthBackground = mBackColor;
| }
|
| mReadOnly = value;
| if(OnReadOnlyChanged !=
| null)
| {
| OnReadOnlyChanged
| (this, new OnReadOnlyChangedEventArgs(value));
| }
| }
| }
|
| [DesignerSerializationVisibility
| (DesignerSerializationVisibility.Visible)]
| public Color LockedColor
| {
| get
| {
| return mLockedColor;
| }
| set
| {
| mLockedColor = value;
| }
| }
|
| #endregion
|
| #region Public Overridden Properties
| public override Color BackColor
| {
| get
| {
| return base.BackColor;
| }
| set
| {
| mBackColor = value;
| if(!mReadOnly)
| {
| base.BackColor =
| value;
| }
| }
| }
| public new OptionalDateTime Value
| {
| set
| {
| if(value.GetValue() == "")
| {
| base.Checked =
| false;
| }
| else
| {
| base.Value =
| DateTime.Parse(value.GetValue());
| }
|
| }
| get
| {
| if(base.Checked)
| {
| return new
| OptionalDateTime(base.Value);
| }
| else
| {
| return new
| OptionalDateTime();
| }
| }
| }
| #endregion
|
| #region Public Overridden Events
|
| protected override void OnValueChanged
| (EventArgs eventargs)
| {
| base.OnValueChanged (eventargs);
|
| if(mReadOnly)
| {
| //We need to set the
| value of the control back to
| //the stored value, since
| it is read only
| if(base.Value !=
| mDateTime)
| {
| base.Value =
| mDateTime;
| }
| if(base.Checked !=
| mChecked)
| {
| base.Checked =
| mChecked;
| }
| }
| else
| {
| //Store the value for
| when it''s read only
| mDateTime = base.Value;
| mChecked = base.Checked;
| }
| }
|
| #endregion
|
| #region Public Methods
| public void Initialise(OptionalDateTime
| Value)
| {
| //Temporarily set the control to
| not ReadOnly.
| bool mTempReadOnly = mReadOnly;
| if(mReadOnly)
| {
| mReadOnly = false;
| }
|
| if(Value.GetValue() == "")
| {
| base.Checked = false;
| mChecked = false;
| }
| else
| {
| base.Value =
| DateTime.Parse(Value.GetValue());
| base.Checked = true;
| mDateTime = base.Value;
| mChecked = true;
| }
|
| //Make sure the ReadOnly value is
| returned to normal
| mReadOnly = mTempReadOnly;
| }
| public void Initialise(DateTime Value)
| {
| Initialise(new OptionalDateTime
| (Value));
| }
| public void Initialise()
| {
| Initialise(new OptionalDateTime
| ());
| }
| #endregion
| }
| }
|
| ##############OptionalDateTime Source##########
|
| [Serializable()]
| public class OptionalDateTime
| {
| #region Enum
| /// <summary>
| /// Formats available - extend as
| required, but remember to update <see cref="GetValue"/>.
| /// </summary>
| public enum enumDateTimeFormat
| {
| /// <summary>
| /// LongDateFormat
| /// </summary>
| LongDateFormat,
| /// <summary>
| /// LongTimeFormat
| /// </summary>
| LongTimeFormat,
| /// <summary>
| /// ShortDateFormat
| /// </summary>
| ShortDateFormat,
| /// <summary>
| /// ShortTimeFormat
| /// </summary>
| ShortTimeFormat
| }
| #endregion
|
| #region Fields
| private DateTime mDate;
| private bool mIsNull;
| #endregion
|
| #region Constructor
| /// <summary>
| /// Constructor - initialises a null
| OptionalDateTime
| /// </summary>
| public OptionalDateTime()
| {
| mIsNull = true;
| }
| /// <summary>
| /// Constructor - initialise an
| OptionalDateTime to contain the value of a string.
| /// If the string is not a valid
| DateTime, the object is set to contain a null date.
| /// </summary>
| /// <param name="value">A string
| representing a valid date.</param>
| public OptionalDateTime(string value)
| {
| SetValue(value);
| }
| /// <summary>
| /// Constructor - initialise an
| OptionalDateTime to contain the value of a DateTime.
| /// </summary>
| /// <param name="value">A DateTime value
| type.</param>
| public OptionalDateTime(DateTime value)
| {
| SetValue(value);
| }
| #endregion
|
| #region Public Methods
| /// <summary>
| /// Set the value of the object to equal
| that of a DateTime.
| /// </summary>
| /// <param name="value">A
| DateTime.</param>
| public void SetValue(DateTime value)
| {
| mDate = value;
| mIsNull = false;
| }
| /// <summary>
| /// Set the value of the object to equal
| that of a string. If the string is not a valid
| /// DateTime, the object is set to
| contain a null date.
| /// </summary>
| /// <param name="value">A string
| representing a valid date.</param>
| public void SetValue(string value)
| {
| if(value == null || value == "")
| {
| mIsNull = true;
| }
| else
| {
| try
| {
| mDate =
| DateTime.Parse(value);
| mIsNull = false;
| }
| catch
| {
| throw new
| ArgumentException("The string entered cannot be converted
| to a DateTime", "value");
| }
| }
| }
| /// <summary>
| /// Return the value of the object as a
| string with optional formatting.
| /// </summary>
| /// <param name="Format">The format to
| return.</param>
| /// <returns>A string containing the
| correctly formatted date.</returns>
| public string GetValue(enumDateTimeFormat
| Format)
| {
| if(mIsNull)
| {
| return "";
| }
| else
| {
| switch(Format)
| {
| case
| enumDateTimeFormat.LongDateFormat:
| return
| mDate.ToLongDateString();
| case
| enumDateTimeFormat.LongTimeFormat:
| return
| mDate.ToLongTimeString();
| case
| enumDateTimeFormat.ShortDateFormat:
| return
| mDate.ToShortDateString();
| case
| enumDateTimeFormat.ShortTimeFormat:
| return
| mDate.ToShortTimeString();
| default:
| throw new
| UnhandledDateFormatException(Format);
| }
| }
| }
| /// <summary>
| /// Return the value of the object as a
| ShortDateString.
| /// </summary>
| /// <returns></returns>
| public string GetValue()
| {
| return GetValue
| (enumDateTimeFormat.ShortDateFormat);
| }
| #endregion
|
| #region Public Override Methods
| /// <summary>
| /// Passes a string containing the date
| in <see cref="enumDateTimeFormat.ShortDateFormat"/>.
| /// </summary>
| /// <returns>The date returned. Passes an
| empty string for blank (null) dates.</returns>
| public override string ToString()
| {
| return GetValue();
| }
| #endregion
|
| #region Properties
| /// <summary>
| /// Returns the date of the
| OptionalDateTime as a DateTime type. Raises the
| /// <see
| cref="OptionalDateTimeIsNullException"/> if the
| OptionalDateTime is
| /// blank (null). Check the <see
| cref="IsNull"/> property before calling this
| /// method to avoid the exception.
| /// </summary>
| public DateTime GetDateTime
| {
| get
| {
| if(mIsNull)
| {
| throw new
| OptionalDateTimeIsNullException();
| }
| return mDate;
| }
| }
| /// <summary>
| /// Gets a boolean value indicating
| whether the OptionalDateTime is blank (null).
| /// </summary>
| public bool IsNull
| {
| get
| {
| return mIsNull;
| }
| }
| #endregion
| }
|
| thanks in advance
|


Ok Jeffrey,生病等待你的回复
Ok Jeffrey, ill wait for your reply
-----原始消息-----


这是一个众所周知的问题。
我会对此做一些研究为您找到一些解决方法
。谢谢您的理解。

致以诚挚的问候,
Jeffrey Tan
微软在线合作伙伴支持
获得安全! - www.microsoft.com/security
此帖子提供就像没有保证,
不授予任何权利。
--------------------
| Content-Class:urn:content-classes:message
|来自:盖伊 < gu*@hcs-ltd.co.uk>
|发件人:盖伊 < gu*@hcs-ltd.co.uk>
|主题:使用Application.EnableVisualStyles()
与datetimepickercontrol的错误
|日期:2003年10月24日星期五03:56:32 -0700
|行:540
|消息ID:< 08 **************************** @ phx.gbl>
| MIME版本:1.0
|内容类型:text / plain;
| charset =" iso-8859-1"
| Content-Transfer-Encoding:7bit
| X-Newsreader:适用于Windows 2000的Microsoft CDO
| Thread-Index:AcOaHXwtoiMaL / QOSWuDQUgjMGLnAw ==
| X-MimeOLE:由Microsoft MimeOLE制作
V5.50.4910.0300 |新闻组:microsoft.public.dotnet.languages.csharp
|路径:cpmsftngxa06.phx.gbl
|外翻:cpmsftngxa06.phx.gbl
microsoft.public.dotnet.languages.csharp:193774 | NNTP-Posting-Host:TK2MSFTNGXA13 10.40.1.165
| X-Tomcat-NG:microsoft.public.dotnet.languages.csharp
|
|我已将datetimepicker控件扩展为
incorp | ReadOnly属性。
|我使用new关键字来实现我自己的
版本value属性,如果readonly == true
那么|它不会设置控件的值,并且
将离开|当
用户|时,复选框的选中状态为false选择一个新的日期。
|
|这在win2k
机器上使用控件时工作正常但如果我们在win XP盒子上使用它并致电
| Application.EnableVisualStyles()然后似乎
忽略|我的代码并选中复选框并设置值。这个
是| extremely buggy behaviour! and also the value property
| gets called twice (only once on a win2k box).
|
| i have also noticed behaviour changes in the
| label.textalignment property when using XP Visual
styles|以及。 Below is my implementation of the extended
| datetimepicker and also the new data type
| (OptionalDateTime) that is used for the value
property...|
| #####LockableDateTimePicker Source###########
|
| using System;
| using System.Collections;
| using System.ComponentModel;
| using System.Drawing;
| using System.Data;
| using System.Windows.Forms;
| using HCS.DataTypes;
|
| namespace HCS.Generic.UI.Controls
| {
| /// <summary>
| /// LockableDateTimePicker is for selecting
| dates. It manipulates the OptionalDateTime
| /// class to allow null dates to be handled.
| /// </summary>
| public class LockableDateTimePicker :
| DateTimePicker
| {
| #region Clean Up Code
|
| /// <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
|
| #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()
| {
| components = new
| System.ComponentModel.Container();
| }
| #endregion
|
| #region Fields
|
| private System.ComponentModel.Container
| components = null;
| private bool mReadOnly;
| private DateTime mDateTime;
| private Color mBackColor;
| private Color mLockedColor;
| private bool mChecked;
|
| #endregion
|
| #region Events
|
| public event OnReadOnlyChangedDelegate
| OnReadOnlyChanged;
|
| #endregion
|
| #region EventArgs and Delegates
|
| public class OnReadOnlyChangedEventArgs :
| EventArgs
| {
| private bool mReadOnly;
|
| public OnReadOnlyChangedEventArgs
| (bool ReadOnly)
| {
| mReadOnly = ReadOnly;
| }
| public bool ReadOnly
| {
| get
| {
| return mReadOnly;
| }
| }
| }
| public delegate void
| OnReadOnlyChangedDelegate(object sender,
| OnReadOnlyChangedEventArgs e);
|
| #endregion
|
| #region Constructor
|
| public LockableDateTimePicker()
| {
| // This call is required by the
| Windows.Forms Form Designer.
| InitializeComponent();
|
| mBackColor =
| base.CalendarMonthBackground;
| mLockedColor =
| base.CalendarMonthBackground;
|
| //Set defaults for this control
| base.Format =
| DateTimePickerFormat.Short;
|
| //Make sure that our date backup
| is populated
| mDateTime = base.Value;
| mChecked = base.Checked;
| }
|
| #endregion
|
| #region Properties
|
| [DesignerSerializationVisibility
| (DesignerSerializationVisibility.Visible)]
| public bool ReadOnly
| {
| get
| {
| return mReadOnly;
| }
| set
| {
| if(value)
| {
|
| base.CalendarMonthBackground = mLockedColor;
| }
|否则
| {
|
| base.CalendarMonthBackground = mBackColor;
| }
|
| mReadOnly = value;
| if(OnReadOnlyChanged !=
| null)
| {
| OnReadOnlyChanged
| (this, new OnReadOnlyChangedEventArgs(value));
| }
| }
| }
|
| [DesignerSerializationVisibility
| (DesignerSerializationVisibility.Visible)]
| public Color LockedColor
| {
| get
| {
| return mLockedColor;
| }
| set
| {
| mLockedColor = value;
| }
| }
|
| #endregion
|
| #region Public Overridden Properties
| public override Color BackColor
| {
| get
| {
| return base.BackColor;
| }
| set
| {
| mBackColor = value;
| if(!mReadOnly)
| {
| base.BackColor =
| value;
| }
| }
| }
| public new OptionalDateTime Value
| {
| set
| {
| if(value.GetValue() == "")
| {
| base.Checked =
| false;
| }
|否则
| {
| base.Value =
| DateTime.Parse(value.GetValue());
| }
|
| }
| get
| {
| if(base.Checked)
| {
| return new
| OptionalDateTime(base.Value);
| }
|否则
| {
| return new
| OptionalDateTime();
| }
| }
| }
| #endregion
|
| #region Public Overridden Events
|
| protected override void OnValueChanged
| (EventArgs eventargs)
| {
| base.OnValueChanged (eventargs);
|
| if(mReadOnly)
| {
| //We need to set the
| value of the control back to
| //the stored value, since
| it is read only
| if(base.Value !=
| mDateTime)
| {
| base.Value =
| mDateTime;
| }
| if(base.Checked !=
| mChecked)
| {
| base.Checked =
| mChecked;
| }
| }
|否则
| {
| //Store the value for
| when it’’s read only
| mDateTime = base.Value;
| mChecked = base.Checked;
| }
| }
|
| #endregion
|
| #region Public Methods
| public void Initialise(OptionalDateTime
| Value)
| {
| //Temporarily set the control to
| not ReadOnly.
| bool mTempReadOnly = mReadOnly;
| if(mReadOnly)
| {
| mReadOnly = false;
| }
|
| if(Value.GetValue() == "")
| {
| base.Checked = false;
| mChecked = false;
| }
|否则
| {
| base.Value =
| DateTime.Parse(Value.GetValue());
| base.Checked = true;
| mDateTime = base.Value;
| mChecked = true;
| }
|
| //Make sure the ReadOnly value is
| returned to normal
| mReadOnly = mTempReadOnly;
| }
| public void Initialise(DateTime Value)
| {
| Initialise(new OptionalDateTime
| (Value));
| }
| public void Initialise()
| {
| Initialise(new OptionalDateTime
| ());
| }
| #endregion
| }
| }
|
| ##############OptionalDateTime Source##########
|
| [Serializable()]
| public class OptionalDateTime
| {
| #region Enum
| /// <summary>
| /// Formats available - extend as
| required, but remember to update <see
cref="GetValue"/>.| /// </summary>
| public enum enumDateTimeFormat
| {
| /// <summary>
| /// LongDateFormat
| /// </summary>
| LongDateFormat,
| /// <summary>
| /// LongTimeFormat
| /// </summary>
| LongTimeFormat,
| /// <summary>
| /// ShortDateFormat
| /// </summary>
| ShortDateFormat,
| /// <summary>
| /// ShortTimeFormat
| /// </summary>
| ShortTimeFormat
| }
| #endregion
|
| #region Fields
| private DateTime mDate;
| private bool mIsNull;
| #endregion
|
| #region Constructor
| /// <summary>
| /// Constructor - initialises a null
| OptionalDateTime
| /// </summary>
| public OptionalDateTime()
| {
| mIsNull = true;
| }
| /// <summary>
| /// Constructor - initialise an
| OptionalDateTime to contain the value of a string.
| /// If the string is not a valid
| DateTime, the object is set to contain a null date.
| /// </summary>
| /// <param name="value">A string
| representing a valid date.</param>
| public OptionalDateTime(string value)
| {
| SetValue(value);
| }
| /// <summary>
| /// Constructor - initialise an
| OptionalDateTime to contain the value of a DateTime.
| /// </summary>
| /// <param name="value">A DateTime value
| type.</param>
| public OptionalDateTime(DateTime value)
| {
| SetValue(value);
| }
| #endregion
|
| #region Public Methods
| /// <summary>
| /// Set the value of the object to equal
| that of a DateTime.
| /// </summary>
| /// <param name="value">A
| DateTime.</param>
| public void SetValue(DateTime value)
| {
| mDate = value;
| mIsNull = false;
| }
| /// <summary>
| /// Set the value of the object to equal
| that of a string. If the string is not a valid
| /// DateTime, the object is set to
| contain a null date.
| /// </summary>
| /// <param name="value">A string
| representing a valid date.</param>
| public void SetValue(string value)
| {
| if(value == null || value == "")
| {
| mIsNull = true;
| }
|否则
| {
| try
| {
| mDate =
| DateTime.Parse(value);
| mIsNull = false;
| }
| catch
| {
| throw new
| ArgumentException("The string entered cannot be
converted| to a DateTime", "value");
| }
| }
| }
| /// <summary>
| /// Return the value of the object as a
| string with optional formatting.
| /// </summary>
| /// <param name="Format">The format to
| return.</param>
| /// <returns>A string containing the
| correctly formatted date.</returns>
| public string GetValue(enumDateTimeFormat
| Format)
| {
| if(mIsNull)
| {
| return "";
| }
|否则
| {
| switch(Format)
| {
| case
| enumDateTimeFormat.LongDateFormat:
| return
| mDate.ToLongDateString();
| case
| enumDateTimeFormat.LongTimeFormat:
| return
| mDate.ToLongTimeString();
| case
| enumDateTimeFormat.ShortDateFormat:
| return
| mDate.ToShortDateString();
| case
| enumDateTimeFormat.ShortTimeFormat:
| return
| mDate.ToShortTimeString();
| default:
| throw new
| UnhandledDateFormatException(Format);
| }
| }
| }
| /// <summary>
| /// Return the value of the object as a
| ShortDateString.
| /// </summary>
| /// <returns></returns>
| public string GetValue()
| {
| return GetValue
| (enumDateTimeFormat.ShortDateFormat);
| }
| #endregion
|
| #region Public Override Methods
| /// <summary>
| /// Passes a string containing the date
| in <see cref="enumDateTimeFormat.ShortDateFormat"/>.
| /// </summary>
| /// <returns>The date returned. Passes an
| empty string for blank (null) dates.</returns>
| public override string ToString()
| {
| return GetValue();
| }
| #endregion
|
| #region Properties
| /// <summary>
| /// Returns the date of the
| OptionalDateTime as a DateTime type. Raises the
| /// <see
| cref="OptionalDateTimeIsNullException"/> if the
| OptionalDateTime is
| /// blank (null). Check the <see
| cref="IsNull"/> property before calling this
| /// method to avoid the exception.
| /// </summary>
| public DateTime GetDateTime
| {
| get
| {
| if(mIsNull)
| {
| throw new
| OptionalDateTimeIsNullException();
| }
| return mDate;
| }
| }
| /// <summary>
| /// Gets a boolean value indicating
| whether the OptionalDateTime is blank (null).
| /// </summary>
| public bool IsNull
| {
| get
| {
| return mIsNull;
| }
| }
| #endregion
| }
|
| thanks in advance
|

.
-----Original Message-----

Hi Guy,

It is an already known issue.
I will do some research on this to find some workaround for you.Thanks for you understanding.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
| Content-Class: urn:content-classes:message
| From: "Guy" <gu*@hcs-ltd.co.uk>
| Sender: "Guy" <gu*@hcs-ltd.co.uk>
| Subject: bug using Application.EnableVisualStyles() with datetimepickercontrol
| Date: Fri, 24 Oct 2003 03:56:32 -0700
| Lines: 540
| Message-ID: <08****************************@phx.gbl>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Thread-Index: AcOaHXwtoiMaL/QOSWuDQUgjMGLnAw==
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300| Newsgroups: microsoft.public.dotnet.languages.csharp
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:193774| NNTP-Posting-Host: TK2MSFTNGXA13 10.40.1.165
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| I have extended the datetimepicker control to incorporate| a ReadOnly property.
| I have used the new keyword to implement my own version| of the value property, so that if readonly == true then| it will not set the value of the control and will leave| the checked status of the checkbox to false when a user| selects a new date.
|
| this works fine when using the control on a win2k machine| but if we use it on a win XP box and call
| Application.EnableVisualStyles() then it seems to ignore| my code and check the checkbox and set the value. this is| extremely buggy behaviour! and also the value property
| gets called twice (only once on a win2k box).
|
| i have also noticed behaviour changes in the
| label.textalignment property when using XP Visual styles| aswell. Below is my implementation of the extended
| datetimepicker and also the new data type
| (OptionalDateTime) that is used for the value property...|
| #####LockableDateTimePicker Source###########
|
| using System;
| using System.Collections;
| using System.ComponentModel;
| using System.Drawing;
| using System.Data;
| using System.Windows.Forms;
| using HCS.DataTypes;
|
| namespace HCS.Generic.UI.Controls
| {
| /// <summary>
| /// LockableDateTimePicker is for selecting
| dates. It manipulates the OptionalDateTime
| /// class to allow null dates to be handled.
| /// </summary>
| public class LockableDateTimePicker :
| DateTimePicker
| {
| #region Clean Up Code
|
| /// <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
|
| #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()
| {
| components = new
| System.ComponentModel.Container();
| }
| #endregion
|
| #region Fields
|
| private System.ComponentModel.Container
| components = null;
| private bool mReadOnly;
| private DateTime mDateTime;
| private Color mBackColor;
| private Color mLockedColor;
| private bool mChecked;
|
| #endregion
|
| #region Events
|
| public event OnReadOnlyChangedDelegate
| OnReadOnlyChanged;
|
| #endregion
|
| #region EventArgs and Delegates
|
| public class OnReadOnlyChangedEventArgs :
| EventArgs
| {
| private bool mReadOnly;
|
| public OnReadOnlyChangedEventArgs
| (bool ReadOnly)
| {
| mReadOnly = ReadOnly;
| }
| public bool ReadOnly
| {
| get
| {
| return mReadOnly;
| }
| }
| }
| public delegate void
| OnReadOnlyChangedDelegate(object sender,
| OnReadOnlyChangedEventArgs e);
|
| #endregion
|
| #region Constructor
|
| public LockableDateTimePicker()
| {
| // This call is required by the
| Windows.Forms Form Designer.
| InitializeComponent();
|
| mBackColor =
| base.CalendarMonthBackground;
| mLockedColor =
| base.CalendarMonthBackground;
|
| //Set defaults for this control
| base.Format =
| DateTimePickerFormat.Short;
|
| //Make sure that our date backup
| is populated
| mDateTime = base.Value;
| mChecked = base.Checked;
| }
|
| #endregion
|
| #region Properties
|
| [DesignerSerializationVisibility
| (DesignerSerializationVisibility.Visible)]
| public bool ReadOnly
| {
| get
| {
| return mReadOnly;
| }
| set
| {
| if(value)
| {
|
| base.CalendarMonthBackground = mLockedColor;
| }
| else
| {
|
| base.CalendarMonthBackground = mBackColor;
| }
|
| mReadOnly = value;
| if(OnReadOnlyChanged !=
| null)
| {
| OnReadOnlyChanged
| (this, new OnReadOnlyChangedEventArgs(value));
| }
| }
| }
|
| [DesignerSerializationVisibility
| (DesignerSerializationVisibility.Visible)]
| public Color LockedColor
| {
| get
| {
| return mLockedColor;
| }
| set
| {
| mLockedColor = value;
| }
| }
|
| #endregion
|
| #region Public Overridden Properties
| public override Color BackColor
| {
| get
| {
| return base.BackColor;
| }
| set
| {
| mBackColor = value;
| if(!mReadOnly)
| {
| base.BackColor =
| value;
| }
| }
| }
| public new OptionalDateTime Value
| {
| set
| {
| if(value.GetValue() == "")
| {
| base.Checked =
| false;
| }
| else
| {
| base.Value =
| DateTime.Parse(value.GetValue());
| }
|
| }
| get
| {
| if(base.Checked)
| {
| return new
| OptionalDateTime(base.Value);
| }
| else
| {
| return new
| OptionalDateTime();
| }
| }
| }
| #endregion
|
| #region Public Overridden Events
|
| protected override void OnValueChanged
| (EventArgs eventargs)
| {
| base.OnValueChanged (eventargs);
|
| if(mReadOnly)
| {
| //We need to set the
| value of the control back to
| //the stored value, since
| it is read only
| if(base.Value !=
| mDateTime)
| {
| base.Value =
| mDateTime;
| }
| if(base.Checked !=
| mChecked)
| {
| base.Checked =
| mChecked;
| }
| }
| else
| {
| //Store the value for
| when it''s read only
| mDateTime = base.Value;
| mChecked = base.Checked;
| }
| }
|
| #endregion
|
| #region Public Methods
| public void Initialise(OptionalDateTime
| Value)
| {
| //Temporarily set the control to
| not ReadOnly.
| bool mTempReadOnly = mReadOnly;
| if(mReadOnly)
| {
| mReadOnly = false;
| }
|
| if(Value.GetValue() == "")
| {
| base.Checked = false;
| mChecked = false;
| }
| else
| {
| base.Value =
| DateTime.Parse(Value.GetValue());
| base.Checked = true;
| mDateTime = base.Value;
| mChecked = true;
| }
|
| //Make sure the ReadOnly value is
| returned to normal
| mReadOnly = mTempReadOnly;
| }
| public void Initialise(DateTime Value)
| {
| Initialise(new OptionalDateTime
| (Value));
| }
| public void Initialise()
| {
| Initialise(new OptionalDateTime
| ());
| }
| #endregion
| }
| }
|
| ##############OptionalDateTime Source##########
|
| [Serializable()]
| public class OptionalDateTime
| {
| #region Enum
| /// <summary>
| /// Formats available - extend as
| required, but remember to update <see cref="GetValue"/>.| /// </summary>
| public enum enumDateTimeFormat
| {
| /// <summary>
| /// LongDateFormat
| /// </summary>
| LongDateFormat,
| /// <summary>
| /// LongTimeFormat
| /// </summary>
| LongTimeFormat,
| /// <summary>
| /// ShortDateFormat
| /// </summary>
| ShortDateFormat,
| /// <summary>
| /// ShortTimeFormat
| /// </summary>
| ShortTimeFormat
| }
| #endregion
|
| #region Fields
| private DateTime mDate;
| private bool mIsNull;
| #endregion
|
| #region Constructor
| /// <summary>
| /// Constructor - initialises a null
| OptionalDateTime
| /// </summary>
| public OptionalDateTime()
| {
| mIsNull = true;
| }
| /// <summary>
| /// Constructor - initialise an
| OptionalDateTime to contain the value of a string.
| /// If the string is not a valid
| DateTime, the object is set to contain a null date.
| /// </summary>
| /// <param name="value">A string
| representing a valid date.</param>
| public OptionalDateTime(string value)
| {
| SetValue(value);
| }
| /// <summary>
| /// Constructor - initialise an
| OptionalDateTime to contain the value of a DateTime.
| /// </summary>
| /// <param name="value">A DateTime value
| type.</param>
| public OptionalDateTime(DateTime value)
| {
| SetValue(value);
| }
| #endregion
|
| #region Public Methods
| /// <summary>
| /// Set the value of the object to equal
| that of a DateTime.
| /// </summary>
| /// <param name="value">A
| DateTime.</param>
| public void SetValue(DateTime value)
| {
| mDate = value;
| mIsNull = false;
| }
| /// <summary>
| /// Set the value of the object to equal
| that of a string. If the string is not a valid
| /// DateTime, the object is set to
| contain a null date.
| /// </summary>
| /// <param name="value">A string
| representing a valid date.</param>
| public void SetValue(string value)
| {
| if(value == null || value == "")
| {
| mIsNull = true;
| }
| else
| {
| try
| {
| mDate =
| DateTime.Parse(value);
| mIsNull = false;
| }
| catch
| {
| throw new
| ArgumentException("The string entered cannot be converted| to a DateTime", "value");
| }
| }
| }
| /// <summary>
| /// Return the value of the object as a
| string with optional formatting.
| /// </summary>
| /// <param name="Format">The format to
| return.</param>
| /// <returns>A string containing the
| correctly formatted date.</returns>
| public string GetValue(enumDateTimeFormat
| Format)
| {
| if(mIsNull)
| {
| return "";
| }
| else
| {
| switch(Format)
| {
| case
| enumDateTimeFormat.LongDateFormat:
| return
| mDate.ToLongDateString();
| case
| enumDateTimeFormat.LongTimeFormat:
| return
| mDate.ToLongTimeString();
| case
| enumDateTimeFormat.ShortDateFormat:
| return
| mDate.ToShortDateString();
| case
| enumDateTimeFormat.ShortTimeFormat:
| return
| mDate.ToShortTimeString();
| default:
| throw new
| UnhandledDateFormatException(Format);
| }
| }
| }
| /// <summary>
| /// Return the value of the object as a
| ShortDateString.
| /// </summary>
| /// <returns></returns>
| public string GetValue()
| {
| return GetValue
| (enumDateTimeFormat.ShortDateFormat);
| }
| #endregion
|
| #region Public Override Methods
| /// <summary>
| /// Passes a string containing the date
| in <see cref="enumDateTimeFormat.ShortDateFormat"/>.
| /// </summary>
| /// <returns>The date returned. Passes an
| empty string for blank (null) dates.</returns>
| public override string ToString()
| {
| return GetValue();
| }
| #endregion
|
| #region Properties
| /// <summary>
| /// Returns the date of the
| OptionalDateTime as a DateTime type. Raises the
| /// <see
| cref="OptionalDateTimeIsNullException"/> if the
| OptionalDateTime is
| /// blank (null). Check the <see
| cref="IsNull"/> property before calling this
| /// method to avoid the exception.
| /// </summary>
| public DateTime GetDateTime
| {
| get
| {
| if(mIsNull)
| {
| throw new
| OptionalDateTimeIsNullException();
| }
| return mDate;
| }
| }
| /// <summary>
| /// Gets a boolean value indicating
| whether the OptionalDateTime is blank (null).
| /// </summary>
| public bool IsNull
| {
| get
| {
| return mIsNull;
| }
| }
| #endregion
| }
|
| thanks in advance
|

.





Hi Guy,


Sorry for letting you waiting so long time.

In this time, I did a lot of research into DateTimePicker control and

wanted to find a workaround for you.

Here, I provide you 2 ways of workaround:

First: override its OnCloseUp method and reset its value to its original

value. But this is not enough, when you open the datetimepicker, you can

use keyboard arrow to navigate the selection, and the value displayed will

also change in the textbox( For the moment, we call it textbox). I think we

should not let this happen, the value should stay the same. But just reset

value in the OnValueChanged method seems work well on keyboard navigation,

but will again change the value when closing the calendar.(It seems that

the OnCloseUp abated). So I hooked into this control, and filter its

MCN_SELCHANGE notification in WM_NOTIFY message.


using System.Runtime .InteropServices ;

public bool readonlyval=false;

private DateTime oldval;


protected override void OnCloseUp(EventArgs eventargs)

{

if(readonlyval==true)

{

this.Value =oldval;

}

oldval=this.Value ;

base.OnCloseUp (eventargs);

}


private const int MCN_FIRST=-750;

private const int MCN_SELCHANGE=MCN_FIRST + 1;

private int WM_NOTIFY=0x004E;


[StructLayout(LayoutKind.Sequential)]

public struct NMHDR

{

public IntPtr hwndFrom;

public int idfrom;

public int code;

}


protected override void WndProc(ref Message m)

{

if(m.Msg==WM_NOTIFY)

{

if(m .HWnd==this.Handle)

{

NMHDR nm1=(NMHDR)Marshal.PtrToStructure(m.LParam,typeof( NMHDR));

if(nm1.code==MCN_SELCHANGE)

{

return;

}

}

}

base.WndProc (ref m);

}


I use Spy++ to find that the DateTimePicker control will receive WM_NOTIFY

message with MCN_SELCHANGE code when selection changes.

All the const value MCN_SELCHANGE, MCN_FIRST can be found in the C:\Program

Files\Microsoft Visual Studio\VC98\Include directory *.h files.


It works well on my machine. If you have any question, please feel free to

tell me.

I will reply the second way of workaround in another reply post.


Best regards

Jeffrey Tan

Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security

此帖子按原样提供没有保证,也没有赋予任何权利。


--------------------

| Content-Class:urn:content-classes:message

| From: "guy" <gu*@hcs-ltd.co.uk>

| Sender: "guy" <gu*@hcs-ltd.co.uk>

| References: <08****************************@phx.gbl>

<6i**************@cpmsftngxa06.phx.gbl>

| Subject: RE: bug using Application.EnableVisualStyles() with

datetimepicker control

| Date: Tue, 28 Oct 2003 01:26:56 -0800

| Lines: 601

| Message-ID: <08****************************@phx.gbl>

| MIME版本:1.0

|内容类型:text / plain;

| charset =" iso-8859-1"

|内容传输编码:7位

| X-Newsreader:适用于Windows 2000的Microsoft CDO

| Thread-Index: AcOdNaFMO+yabmhBTea+z5L0ii9kXg==

| X-MimeOLE:由Microsoft MimeOLE制作V5.50.4910.0300

|新闻组:microsoft.public.dotnet.languages.csharp

|路径:cpmsftngxa06.phx.gbl

| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:194627

| NNTP-Posting-Host: TK2MSFTNGXA14 10.40.1.166

| X-Tomcat-NG:microsoft.public.dotnet.languages.csharp

|

| Ok Jeffrey, ill wait for your reply

|

|

|

| >-----Original Message-----

| >

| >Hi Guy,

| >

| >It is an already known issue.

| >I will do some research on this to find some workaround

| for you.

| >Thanks for you understanding.

| >

| >Best regards,

| >Jeffrey Tan

| >Microsoft Online Partner Support

| >安全! - www.microsoft.com/security

| >This posting is provided "as is" with no warranties and

| confers no rights.

| >

| > --------------------

| > | Content-Class:urn:content-classes:message

| > | From: "Guy" <gu*@hcs-ltd.co.uk>

| > | Sender: "Guy" <gu*@hcs-ltd.co.uk>

| > | Subject: bug using Application.EnableVisualStyles()

| with datetimepicker

| >control

| > | Date: Fri, 24 Oct 2003 03:56:32 -0700

| > | Lines: 540

| > | Message-ID: <08****************************@phx.gbl>

| > | MIME版本:1.0

| > |内容类型:text / plain;

| > | charset =" iso-8859-1"

| > |内容传输编码:7位

| > | X-Newsreader:适用于Windows 2000的Microsoft CDO

| > | Thread-Index: AcOaHXwtoiMaL/QOSWuDQUgjMGLnAw==

| > | X-MimeOLE: Produced By Microsoft MimeOLE

| V5.50.4910.0300

| > |新闻组:microsoft.public.dotnet.languages.csharp

| > |路径:cpmsftngxa06.phx.gbl

| > | Xref: cpmsftngxa06.phx.gbl

| microsoft.public.dotnet.languages.csharp:193774

| > | NNTP-Posting-Host: TK2MSFTNGXA13 10.40.1.165

| > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

| > |

| > | I have extended the datetimepicker control to

| incorporate

| > | a ReadOnly property.

| > | I have used the new keyword to implement my own

| version

| > | of the value property, so that if readonly == true

| then

| > | it will not set the value of the control and will

| leave

| > | the checked status of the checkbox to false when a

|用户

| > | selects a new date.

| > |

| > | this works fine when using the control on a win2k

| machine

| > | but if we use it on a win XP box and call

| > | Application.EnableVisualStyles() then it seems to

| ignore

| > | my code and check the checkbox and set the value. this

| is

| > | extremely buggy behaviour! and also the value property

| > | gets called twice (only once on a win2k box).

| > |

| > | i have also noticed behaviour changes in the

| > | label.textalignment property when using XP Visual

| styles

| > |以及。 Below is my implementation of the extended

| > | datetimepicker and also the new data type

| > | (OptionalDateTime) that is used for the value

| property...

| > |

| > | #####LockableDateTimePicker Source###########

| > |

| > |使用系统;

| > |使用System.Collections;

| > |使用System.ComponentModel;

| > |使用System.Drawing;

| > |使用System.Data;

| > |使用System.Windows.Forms;

| > | using HCS.DataTypes;

| > |

| > | namespace HCS.Generic.UI.Controls

| > | {

| > | /// <summary>

| > | /// LockableDateTimePicker is for selecting

| > | dates. It manipulates the OptionalDateTime

| > | /// class to allow null dates to be handled.

| > | /// </summary>

| > | public class LockableDateTimePicker :

| > | DateTimePicker

| > | {

| > | #region Clean Up Code

| > |

| > | /// <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

| > |

| > | #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()

| > | {

| > | components = new

| > | System.ComponentModel.Container();

| > | }

| > | #endregion

| > |

| > | #region Fields

| > |

| > | private System.ComponentModel.Container

| > | components = null;

| > | private bool mReadOnly;

| > | private DateTime mDateTime;

| > | private Color mBackColor;

| > | private Color mLockedColor;

| > | private bool mChecked;

| > |

| > | #endregion

| > |

| > | #region Events

| > |

| > | public event OnReadOnlyChangedDelegate

| > | OnReadOnlyChanged;

| > |

| > | #endregion

| > |

| > | #region EventArgs and Delegates

| > |

| > | public class OnReadOnlyChangedEventArgs :

| > | EventArgs

| > | {

| > | private bool mReadOnly;

| > |

| > | public OnReadOnlyChangedEventArgs

| > | (bool ReadOnly)

| > | {

| > | mReadOnly = ReadOnly;

| > | }

| > | public bool ReadOnly

| > | {

| > |得到

| > | {

| > | return mReadOnly;

| > | }

| > | }

| > | }

| > | public delegate void

| > | OnReadOnlyChangedDelegate(object sender,

| >| OnReadOnlyChangedEventArgs e);

| > |

| > | #endregion

| > |

| > | #region Constructor

| > |

| > | public LockableDateTimePicker()

| > | {

| > | // This call is required by the

| > | Windows.Forms Form Designer.

| > | InitializeComponent();

| > |

| > | mBackColor =

| > | base.CalendarMonthBackground;

| > | mLockedColor =

| > | base.CalendarMonthBackground;

| > |

| > | //Set defaults for this control

| > | base.Format =

| > | DateTimePickerFormat.Short;

| > |

| > | //Make sure that our date backup

| > | is populated

| > | mDateTime = base.Value;

| > | mChecked = base.Checked;

| > | }

| > |

| > | #endregion

| > |

| > | #region Properties

| > |

| > | [DesignerSerializationVisibility

| > | (DesignerSerializationVisibility.Visible)]

| > | public bool ReadOnly

| > | {

| > |得到

| > | {

| > | return mReadOnly;

| > | }

| > |设置

| > | {

| > | if(value)

| > | {

| > |

| > | base.CalendarMonthBackground = mLockedColor;

| > | }

| > | else

| > | {

| > |

| > | base.CalendarMonthBackground = mBackColor;

| > | }

| > |

| > | mReadOnly = value;

| > | if(OnReadOnlyChanged !=

| >| null)

| > | {

| > | OnReadOnlyChanged

| > | (this, new OnReadOnlyChangedEventArgs(value));

| > | }

| > | }

| > | }

| > |

| > | [DesignerSerializationVisibility

| > | (DesignerSerializationVisibility.Visible)]

| > | public Color LockedColor

| > | {

| > |得到

| > | {

| > | return mLockedColor;

| > | }

| > |设置

| > | {

| > | mLockedColor = value;

| > | }

| > | }

| > |

| > | #endregion

| > |

| > | #region Public Overridden Properties

| > | public override Color BackColor

| > | {

| > |得到

| > | {

| > | return base.BackColor;

| > | }

| > |设置

| > | {

| > | mBackColor = value;

| > | if(!mReadOnly)

| > | {

| > | base.BackColor =

| > | value;

| > | }

| > | }

| > | }

| > | public new OptionalDateTime Value

| > | {

| > |设置

| > | {

| > | if(value.GetValue() == "")

| > | {

| > | base.Checked =

| > | false;

| > | }

| > | else

| > | {

| > | base.Value =

| > | DateTime.Parse(value.GetValue());

| > | }

| > |

| > | }

| > |得到

| > | {

| > | if(base.Checked)

| > | {

| > | return new

| > | OptionalDateTime(base.Value);

| > | }

| > | else

| > | {

| > | return new

| > | OptionalDateTime();

| > | }

| > | }

| > | }

| > | #endregion

| > |

| > | #region Public Overridden Events

| > |

| > | protected override void OnValueChanged

| > | (EventArgs eventargs)

| > | {

| > | base.OnValueChanged (eventargs);

| > |

| > | if(mReadOnly)

| > | {

| > | //We need to set the

| > | value of the control back to

| > | //the stored value, since

| > | it is read only

| > | if(base.Value !=

| >| mDateTime)

| > | {

| > | base.Value =

| > | mDateTime;

| > | }

| > | if(base.Checked !=

| >| mChecked)

| > | {

| > | base.Checked =

| > | mChecked;

| > | }

| > | }

| > | else

| > | {

| > | //Store the value for

| > | when it’’s read only

| > | mDateTime = base.Value;

| > | mChecked = base.Checked;

| > | }

| > | }

| > |

| > | #endregion

| > |

| > | #region Public Methods

| > | public void Initialise(OptionalDateTime

| >| Value)

| > | {

| > | //Temporarily set the control to

| > | not ReadOnly.

| > | bool mTempReadOnly = mReadOnly;

| > | if(mReadOnly)

| > | {

| > | mReadOnly = false;

| > | }

| > |

| > | if(Value.GetValue() == "")

| > | {

| > | base.Checked = false;

| > | mChecked = false;

| > | }

| > | else

| > | {

| > | base.Value =

| > | DateTime.Parse(Value.GetValue());

| > | base.Checked = true;

| > | mDateTime = base.Value;

| > | mChecked = true;

| > | }

| > |

| > | //Make sure the ReadOnly value is

| > | returned to normal

| > | mReadOnly = mTempReadOnly;

| > | }

| > | public void Initialise(DateTime Value)

| > | {

| > | Initialise(new OptionalDateTime

| >| (Value));

| > | }

| > | public void Initialise()

| > | {

| > | Initialise(new OptionalDateTime

| >| ());

| > | }

| > | #endregion

| > | }

| > | }

| > |

| > | ##############OptionalDateTime Source##########

| > |

| > | [Serializable()]

| > | public class OptionalDateTime

| > | {

| > | #region Enum

| > | /// <summary>

| > | /// Formats available - extend as

| > | required, but remember to update <see

| cref="GetValue"/>.

| > | /// </summary>

| > | public enum enumDateTimeFormat

| > | {

| > | /// <summary>

| > | /// LongDateFormat

| > | /// </summary>

| > | LongDateFormat,

| > | /// <summary>

| > | /// LongTimeFormat

| > | /// </summary>

| > | LongTimeFormat,

| > | /// <summary>

| > | /// ShortDateFormat

| > | /// </summary>

| > | ShortDateFormat,

| > | /// <summary>

| > | /// ShortTimeFormat

| > | /// </summary>

| > | ShortTimeFormat

| > | }

| > | #endregion

| > |

| > | #region Fields

| > | private DateTime mDate;

| > | private bool mIsNull;

| > | #endregion

| > |

| > | #region Constructor

| > | /// <summary>

| > | /// Constructor - initialises a null

| > | OptionalDateTime

| > | /// </summary>

| > | public OptionalDateTime()

| > | {

| > | mIsNull = true;

| > | }

| > | /// <summary>

| > | /// Constructor - initialise an

| > | OptionalDateTime to contain the value of a string.

| > | /// If the string is not a valid

| > | DateTime, the object is set to contain a null date.

| > | /// </summary>

| > | /// <param name="value">A string

| > | representing a valid date.</param>

| > | public OptionalDateTime(string value)

| > | {

| > | SetValue(value);

| > | }

| > | /// <summary>

| > | /// Constructor - initialise an

| > | OptionalDateTime to contain the value of a DateTime.

| > | /// </summary>

| > | /// <param name="value">A DateTime value

| > | type.</param>

| > | public OptionalDateTime(DateTime value)

| > | {

| > | SetValue(value);

| > | }

| > | #endregion

| > |

| > | #region Public Methods

| > | /// <summary>

| > | /// Set the value of the object to equal

| > | that of a DateTime.

| > | /// </summary>

| > | /// <param name="value">A

| > | DateTime.</param>

| > | public void SetValue(DateTime value)

| > | {

| > | mDate = value;

| > | mIsNull = false;

| > | }

| > | /// <summary>

| > | /// Set the value of the object to equal

| > | that of a string. If the string is not a valid

| > | /// DateTime, the object is set to

| > | contain a null date.

| > | /// </summary>

| > | /// <param name="value">A string

| > | representing a valid date.</param>

| > | public void SetValue(string value)

| > | {

| > | if(value == null || value == "")

| > | {

| > | mIsNull = true;

| > | }

| > | else

| > | {

| > |试试

| > | {

| > | mDate =

| > | DateTime.Parse(value);

| > | mIsNull = false;

| > | }

| > | catch

| > | {

| > | throw new

| > | ArgumentException("The string entered cannot be

| converted

| >| to a DateTime", "value");

| > | }

| > | }

| > | }

| > | /// <summary>

| > | /// Return the value of the object as a

| > | string with optional formatting.

| > | /// </summary>

| > | /// <param name="Format">The format to

| > | return.</param>

| > | /// <returns>A string containing the

| > | correctly formatted date.</returns>

| > | public string GetValue(enumDateTimeFormat

| >| Format)

| > | {

| > | if(mIsNull)

| > | {

| > | return "";

| > | }

| > | else

| > | {

| > | switch(Format)

| > | {

| > | case

| > | enumDateTimeFormat.LongDateFormat:

| > | return

| > | mDate.ToLongDateString();

| > | case

| > | enumDateTimeFormat.LongTimeFormat:

| > | return

| > | mDate.ToLongTimeString();

| > | case

| > | enumDateTimeFormat.ShortDateFormat:

| > | return

| > | mDate.ToShortDateString();

| > | case

| > | enumDateTimeFormat.ShortTimeFormat:

| > | return

| > | mDate.ToShortTimeString();

| > |默认值:

| > | throw new

| > | UnhandledDateFormatException(Format);

| > | }

| > | }

| > | }

| > | /// <summary>

| > | /// Return the value of the object as a

| > | ShortDateString.

| > | /// </summary>

| > | /// <returns></returns>

| > | public string GetValue()

| > | {

| > | return GetValue

| > | (enumDateTimeFormat.ShortDateFormat);

| > | }

| > | #endregion

| > |

| > | #region Public Override Methods

| > | /// <summary>

| > | /// Passes a string containing the date

| > | in <see cref="enumDateTimeFormat.ShortDateFormat"/>.

| > | /// </summary>

| > | /// <returns>The date returned. Passes an

| > | empty string for blank (null) dates.</returns>

| > | public override string ToString()

| > | {

| > | return GetValue();

| > | }

| > | #endregion

| > |

| > | #region Properties

| > | /// <summary>

| > | /// Returns the date of the

| > | OptionalDateTime as a DateTime type. Raises the

| > | /// <see

| > | cref="OptionalDateTimeIsNullException"/> if the

| > | OptionalDateTime is

| > | /// blank (null). Check the <see

| > | cref="IsNull"/> property before calling this

| > | /// method to avoid the exception.

| > | /// </summary>

| > | public DateTime GetDateTime

| > | {

| > |得到

| > | {

| > | if(mIsNull)

| > | {

| > | throw new

| > | OptionalDateTimeIsNullException();

| > | }

| > | return mDate;

| > | }

| > | }

| > | /// <summary>

| > | /// Gets a boolean value indicating

| > | whether the OptionalDateTime is blank (null).

| > | /// </summary>

| > | public bool IsNull

| > | {

| > |得到

| > | {

| > | return mIsNull;

| > | }

| > | }

| > | #endregion

| > | }

| > |

| > | thanks in advance

| > |

| >

| >.

| >

|


Hi Guy,

Sorry for letting you waiting so long time.
In this time, I did a lot of research into DateTimePicker control and
wanted to find a workaround for you.
Here, I provide you 2 ways of workaround:
First: override its OnCloseUp method and reset its value to its original
value. But this is not enough, when you open the datetimepicker, you can
use keyboard arrow to navigate the selection, and the value displayed will
also change in the textbox( For the moment, we call it textbox). I think we
should not let this happen, the value should stay the same. But just reset
value in the OnValueChanged method seems work well on keyboard navigation,
but will again change the value when closing the calendar.(It seems that
the OnCloseUp abated). So I hooked into this control, and filter its
MCN_SELCHANGE notification in WM_NOTIFY message.

using System.Runtime .InteropServices ;
public bool readonlyval=false;
private DateTime oldval;

protected override void OnCloseUp(EventArgs eventargs)
{
if(readonlyval==true)
{
this.Value =oldval;
}
oldval=this.Value ;
base.OnCloseUp (eventargs);
}

private const int MCN_FIRST=-750;
private const int MCN_SELCHANGE=MCN_FIRST + 1;
private int WM_NOTIFY=0x004E;

[StructLayout(LayoutKind.Sequential)]
public struct NMHDR
{
public IntPtr hwndFrom;
public int idfrom;
public int code;
}

protected override void WndProc(ref Message m)
{
if(m.Msg==WM_NOTIFY)
{
if(m.HWnd==this.Handle)
{
NMHDR nm1=(NMHDR)Marshal.PtrToStructure(m.LParam,typeof( NMHDR));
if(nm1.code==MCN_SELCHANGE)
{
return;
}
}
}
base.WndProc (ref m);
}

I use Spy++ to find that the DateTimePicker control will receive WM_NOTIFY
message with MCN_SELCHANGE code when selection changes.
All the const value MCN_SELCHANGE, MCN_FIRST can be found in the C:\Program
Files\Microsoft Visual Studio\VC98\Include directory *.h files.

It works well on my machine. If you have any question, please feel free to
tell me.
I will reply the second way of workaround in another reply post.

Best regards
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Content-Class: urn:content-classes:message
| From: "guy" <gu*@hcs-ltd.co.uk>
| Sender: "guy" <gu*@hcs-ltd.co.uk>
| References: <08****************************@phx.gbl>
<6i**************@cpmsftngxa06.phx.gbl>
| Subject: RE: bug using Application.EnableVisualStyles() with
datetimepicker control
| Date: Tue, 28 Oct 2003 01:26:56 -0800
| Lines: 601
| Message-ID: <08****************************@phx.gbl>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Thread-Index: AcOdNaFMO+yabmhBTea+z5L0ii9kXg==
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:194627
| NNTP-Posting-Host: TK2MSFTNGXA14 10.40.1.166
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Ok Jeffrey, ill wait for your reply
|
|
|
| >-----Original Message-----
| >
| >Hi Guy,
| >
| >It is an already known issue.
| >I will do some research on this to find some workaround
| for you.
| >Thanks for you understanding.
| >
| >Best regards,
| >Jeffrey Tan
| >Microsoft Online Partner Support
| >Get Secure! - www.microsoft.com/security
| >This posting is provided "as is" with no warranties and
| confers no rights.
| >
| >--------------------
| >| Content-Class: urn:content-classes:message
| >| From: "Guy" <gu*@hcs-ltd.co.uk>
| >| Sender: "Guy" <gu*@hcs-ltd.co.uk>
| >| Subject: bug using Application.EnableVisualStyles()
| with datetimepicker
| >control
| >| Date: Fri, 24 Oct 2003 03:56:32 -0700
| >| Lines: 540
| >| Message-ID: <08****************************@phx.gbl>
| >| MIME-Version: 1.0
| >| Content-Type: text/plain;
| >| charset="iso-8859-1"
| >| Content-Transfer-Encoding: 7bit
| >| X-Newsreader: Microsoft CDO for Windows 2000
| >| Thread-Index: AcOaHXwtoiMaL/QOSWuDQUgjMGLnAw==
| >| X-MimeOLE: Produced By Microsoft MimeOLE
| V5.50.4910.0300
| >| Newsgroups: microsoft.public.dotnet.languages.csharp
| >| Path: cpmsftngxa06.phx.gbl
| >| Xref: cpmsftngxa06.phx.gbl
| microsoft.public.dotnet.languages.csharp:193774
| >| NNTP-Posting-Host: TK2MSFTNGXA13 10.40.1.165
| >| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| >|
| >| I have extended the datetimepicker control to
| incorporate
| >| a ReadOnly property.
| >| I have used the new keyword to implement my own
| version
| >| of the value property, so that if readonly == true
| then
| >| it will not set the value of the control and will
| leave
| >| the checked status of the checkbox to false when a
| user
| >| selects a new date.
| >|
| >| this works fine when using the control on a win2k
| machine
| >| but if we use it on a win XP box and call
| >| Application.EnableVisualStyles() then it seems to
| ignore
| >| my code and check the checkbox and set the value. this
| is
| >| extremely buggy behaviour! and also the value property
| >| gets called twice (only once on a win2k box).
| >|
| >| i have also noticed behaviour changes in the
| >| label.textalignment property when using XP Visual
| styles
| >| aswell. Below is my implementation of the extended
| >| datetimepicker and also the new data type
| >| (OptionalDateTime) that is used for the value
| property...
| >|
| >| #####LockableDateTimePicker Source###########
| >|
| >| using System;
| >| using System.Collections;
| >| using System.ComponentModel;
| >| using System.Drawing;
| >| using System.Data;
| >| using System.Windows.Forms;
| >| using HCS.DataTypes;
| >|
| >| namespace HCS.Generic.UI.Controls
| >| {
| >| /// <summary>
| >| /// LockableDateTimePicker is for selecting
| >| dates. It manipulates the OptionalDateTime
| >| /// class to allow null dates to be handled.
| >| /// </summary>
| >| public class LockableDateTimePicker :
| >| DateTimePicker
| >| {
| >| #region Clean Up Code
| >|
| >| /// <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
| >|
| >| #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()
| >| {
| >| components = new
| >| System.ComponentModel.Container();
| >| }
| >| #endregion
| >|
| >| #region Fields
| >|
| >| private System.ComponentModel.Container
| >| components = null;
| >| private bool mReadOnly;
| >| private DateTime mDateTime;
| >| private Color mBackColor;
| >| private Color mLockedColor;
| >| private bool mChecked;
| >|
| >| #endregion
| >|
| >| #region Events
| >|
| >| public event OnReadOnlyChangedDelegate
| >| OnReadOnlyChanged;
| >|
| >| #endregion
| >|
| >| #region EventArgs and Delegates
| >|
| >| public class OnReadOnlyChangedEventArgs :
| >| EventArgs
| >| {
| >| private bool mReadOnly;
| >|
| >| public OnReadOnlyChangedEventArgs
| >| (bool ReadOnly)
| >| {
| >| mReadOnly = ReadOnly;
| >| }
| >| public bool ReadOnly
| >| {
| >| get
| >| {
| >| return mReadOnly;
| >| }
| >| }
| >| }
| >| public delegate void
| >| OnReadOnlyChangedDelegate(object sender,
| >| OnReadOnlyChangedEventArgs e);
| >|
| >| #endregion
| >|
| >| #region Constructor
| >|
| >| public LockableDateTimePicker()
| >| {
| >| // This call is required by the
| >| Windows.Forms Form Designer.
| >| InitializeComponent();
| >|
| >| mBackColor =
| >| base.CalendarMonthBackground;
| >| mLockedColor =
| >| base.CalendarMonthBackground;
| >|
| >| //Set defaults for this control
| >| base.Format =
| >| DateTimePickerFormat.Short;
| >|
| >| //Make sure that our date backup
| >| is populated
| >| mDateTime = base.Value;
| >| mChecked = base.Checked;
| >| }
| >|
| >| #endregion
| >|
| >| #region Properties
| >|
| >| [DesignerSerializationVisibility
| >| (DesignerSerializationVisibility.Visible)]
| >| public bool ReadOnly
| >| {
| >| get
| >| {
| >| return mReadOnly;
| >| }
| >| set
| >| {
| >| if(value)
| >| {
| >|
| >| base.CalendarMonthBackground = mLockedColor;
| >| }
| >| else
| >| {
| >|
| >| base.CalendarMonthBackground = mBackColor;
| >| }
| >|
| >| mReadOnly = value;
| >| if(OnReadOnlyChanged !=
| >| null)
| >| {
| >| OnReadOnlyChanged
| >| (this, new OnReadOnlyChangedEventArgs(value));
| >| }
| >| }
| >| }
| >|
| >| [DesignerSerializationVisibility
| >| (DesignerSerializationVisibility.Visible)]
| >| public Color LockedColor
| >| {
| >| get
| >| {
| >| return mLockedColor;
| >| }
| >| set
| >| {
| >| mLockedColor = value;
| >| }
| >| }
| >|
| >| #endregion
| >|
| >| #region Public Overridden Properties
| >| public override Color BackColor
| >| {
| >| get
| >| {
| >| return base.BackColor;
| >| }
| >| set
| >| {
| >| mBackColor = value;
| >| if(!mReadOnly)
| >| {
| >| base.BackColor =
| >| value;
| >| }
| >| }
| >| }
| >| public new OptionalDateTime Value
| >| {
| >| set
| >| {
| >| if(value.GetValue() == "")
| >| {
| >| base.Checked =
| >| false;
| >| }
| >| else
| >| {
| >| base.Value =
| >| DateTime.Parse(value.GetValue());
| >| }
| >|
| >| }
| >| get
| >| {
| >| if(base.Checked)
| >| {
| >| return new
| >| OptionalDateTime(base.Value);
| >| }
| >| else
| >| {
| >| return new
| >| OptionalDateTime();
| >| }
| >| }
| >| }
| >| #endregion
| >|
| >| #region Public Overridden Events
| >|
| >| protected override void OnValueChanged
| >| (EventArgs eventargs)
| >| {
| >| base.OnValueChanged (eventargs);
| >|
| >| if(mReadOnly)
| >| {
| >| //We need to set the
| >| value of the control back to
| >| //the stored value, since
| >| it is read only
| >| if(base.Value !=
| >| mDateTime)
| >| {
| >| base.Value =
| >| mDateTime;
| >| }
| >| if(base.Checked !=
| >| mChecked)
| >| {
| >| base.Checked =
| >| mChecked;
| >| }
| >| }
| >| else
| >| {
| >| //Store the value for
| >| when it''s read only
| >| mDateTime = base.Value;
| >| mChecked = base.Checked;
| >| }
| >| }
| >|
| >| #endregion
| >|
| >| #region Public Methods
| >| public void Initialise(OptionalDateTime
| >| Value)
| >| {
| >| //Temporarily set the control to
| >| not ReadOnly.
| >| bool mTempReadOnly = mReadOnly;
| >| if(mReadOnly)
| >| {
| >| mReadOnly = false;
| >| }
| >|
| >| if(Value.GetValue() == "")
| >| {
| >| base.Checked = false;
| >| mChecked = false;
| >| }
| >| else
| >| {
| >| base.Value =
| >| DateTime.Parse(Value.GetValue());
| >| base.Checked = true;
| >| mDateTime = base.Value;
| >| mChecked = true;
| >| }
| >|
| >| //Make sure the ReadOnly value is
| >| returned to normal
| >| mReadOnly = mTempReadOnly;
| >| }
| >| public void Initialise(DateTime Value)
| >| {
| >| Initialise(new OptionalDateTime
| >| (Value));
| >| }
| >| public void Initialise()
| >| {
| >| Initialise(new OptionalDateTime
| >| ());
| >| }
| >| #endregion
| >| }
| >| }
| >|
| >| ##############OptionalDateTime Source##########
| >|
| >| [Serializable()]
| >| public class OptionalDateTime
| >| {
| >| #region Enum
| >| /// <summary>
| >| /// Formats available - extend as
| >| required, but remember to update <see
| cref="GetValue"/>.
| >| /// </summary>
| >| public enum enumDateTimeFormat
| >| {
| >| /// <summary>
| >| /// LongDateFormat
| >| /// </summary>
| >| LongDateFormat,
| >| /// <summary>
| >| /// LongTimeFormat
| >| /// </summary>
| >| LongTimeFormat,
| >| /// <summary>
| >| /// ShortDateFormat
| >| /// </summary>
| >| ShortDateFormat,
| >| /// <summary>
| >| /// ShortTimeFormat
| >| /// </summary>
| >| ShortTimeFormat
| >| }
| >| #endregion
| >|
| >| #region Fields
| >| private DateTime mDate;
| >| private bool mIsNull;
| >| #endregion
| >|
| >| #region Constructor
| >| /// <summary>
| >| /// Constructor - initialises a null
| >| OptionalDateTime
| >| /// </summary>
| >| public OptionalDateTime()
| >| {
| >| mIsNull = true;
| >| }
| >| /// <summary>
| >| /// Constructor - initialise an
| >| OptionalDateTime to contain the value of a string.
| >| /// If the string is not a valid
| >| DateTime, the object is set to contain a null date.
| >| /// </summary>
| >| /// <param name="value">A string
| >| representing a valid date.</param>
| >| public OptionalDateTime(string value)
| >| {
| >| SetValue(value);
| >| }
| >| /// <summary>
| >| /// Constructor - initialise an
| >| OptionalDateTime to contain the value of a DateTime.
| >| /// </summary>
| >| /// <param name="value">A DateTime value
| >| type.</param>
| >| public OptionalDateTime(DateTime value)
| >| {
| >| SetValue(value);
| >| }
| >| #endregion
| >|
| >| #region Public Methods
| >| /// <summary>
| >| /// Set the value of the object to equal
| >| that of a DateTime.
| >| /// </summary>
| >| /// <param name="value">A
| >| DateTime.</param>
| >| public void SetValue(DateTime value)
| >| {
| >| mDate = value;
| >| mIsNull = false;
| >| }
| >| /// <summary>
| >| /// Set the value of the object to equal
| >| that of a string. If the string is not a valid
| >| /// DateTime, the object is set to
| >| contain a null date.
| >| /// </summary>
| >| /// <param name="value">A string
| >| representing a valid date.</param>
| >| public void SetValue(string value)
| >| {
| >| if(value == null || value == "")
| >| {
| >| mIsNull = true;
| >| }
| >| else
| >| {
| >| try
| >| {
| >| mDate =
| >| DateTime.Parse(value);
| >| mIsNull = false;
| >| }
| >| catch
| >| {
| >| throw new
| >| ArgumentException("The string entered cannot be
| converted
| >| to a DateTime", "value");
| >| }
| >| }
| >| }
| >| /// <summary>
| >| /// Return the value of the object as a
| >| string with optional formatting.
| >| /// </summary>
| >| /// <param name="Format">The format to
| >| return.</param>
| >| /// <returns>A string containing the
| >| correctly formatted date.</returns>
| >| public string GetValue(enumDateTimeFormat
| >| Format)
| >| {
| >| if(mIsNull)
| >| {
| >| return "";
| >| }
| >| else
| >| {
| >| switch(Format)
| >| {
| >| case
| >| enumDateTimeFormat.LongDateFormat:
| >| return
| >| mDate.ToLongDateString();
| >| case
| >| enumDateTimeFormat.LongTimeFormat:
| >| return
| >| mDate.ToLongTimeString();
| >| case
| >| enumDateTimeFormat.ShortDateFormat:
| >| return
| >| mDate.ToShortDateString();
| >| case
| >| enumDateTimeFormat.ShortTimeFormat:
| >| return
| >| mDate.ToShortTimeString();
| >| default:
| >| throw new
| >| UnhandledDateFormatException(Format);
| >| }
| >| }
| >| }
| >| /// <summary>
| >| /// Return the value of the object as a
| >| ShortDateString.
| >| /// </summary>
| >| /// <returns></returns>
| >| public string GetValue()
| >| {
| >| return GetValue
| >| (enumDateTimeFormat.ShortDateFormat);
| >| }
| >| #endregion
| >|
| >| #region Public Override Methods
| >| /// <summary>
| >| /// Passes a string containing the date
| >| in <see cref="enumDateTimeFormat.ShortDateFormat"/>.
| >| /// </summary>
| >| /// <returns>The date returned. Passes an
| >| empty string for blank (null) dates.</returns>
| >| public override string ToString()
| >| {
| >| return GetValue();
| >| }
| >| #endregion
| >|
| >| #region Properties
| >| /// <summary>
| >| /// Returns the date of the
| >| OptionalDateTime as a DateTime type. Raises the
| >| /// <see
| >| cref="OptionalDateTimeIsNullException"/> if the
| >| OptionalDateTime is
| >| /// blank (null). Check the <see
| >| cref="IsNull"/> property before calling this
| >| /// method to avoid the exception.
| >| /// </summary>
| >| public DateTime GetDateTime
| >| {
| >| get
| >| {
| >| if(mIsNull)
| >| {
| >| throw new
| >| OptionalDateTimeIsNullException();
| >| }
| >| return mDate;
| >| }
| >| }
| >| /// <summary>
| >| /// Gets a boolean value indicating
| >| whether the OptionalDateTime is blank (null).
| >| /// </summary>
| >| public bool IsNull
| >| {
| >| get
| >| {
| >| return mIsNull;
| >| }
| >| }
| >| #endregion
| >| }
| >|
| >| thanks in advance
| >|
| >
| >.
| >
|


这篇关于使用带有datetimepicker控件的Application.EnableVisualStyles()的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆