我想把一个控件封装起来.DateTimePicker
因为他的value的类型为DateTime所以不可以为Null
但是数据库里有一些日期是为空的.如离职日期
现在我写了一个类继承至DateTimePicker
使用public object Value覆盖了基类的Value方法
这样它就可以为NULL了.
在使用时.我使用的是绑定
private MDateTimePicker dtpQuotationDate;
this.dtpQuotationDate.DataBindings.Add("Value", this.bindingSource1, "QuotationDate");
但是他一直只使用我这个类Value的Set方法,不使用我这个类的Value的Get方法,还是调动Base.Value.不知道为什么啊 ,请各位高手指点.谢谢!
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Text.RegularExpressions;
namespace WinUI
{
/// <summary>
/// 用于处理DBNull问题的DateTimePicker
/// </summary>
public class MDateTimePicker :DateTimePicker
{
private object dateValue;
public new string Text
{
get
{
return this.dateValue.ToString();
}
set
{
this.dateValue = value;
}
}
public object Value
{
get
{
if (dateValue==null)
{
Format = DateTimePickerFormat.Custom; this.CustomFormat = " ";
}
else
{
this.Format = DateTimePickerFormat.Short;
}
return dateValue;
}
set
{
dateValue = value;
base.Value = Convert.ToDateTime(value);
}
}
protected override void OnTextChanged(EventArgs e)
{
//base.OnTextChanged(e);
//if (Convert.ToDateTime(Value) == MaxDate)
//{
// Format = DateTimePickerFormat.Custom;
// CustomFormat = " ";
//}
//else
//{
// Format = DateTimePickerFormat.Long;
//}
}
protected override void OnClick(EventArgs e)
{
//base.OnClick(e);
//onEdit();
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (e.KeyCode == Keys.Delete)
{
this.Value = null;
}
else
{
//onEdit();
}
}
private void onEdit()
{
Format = DateTimePickerFormat.Long;
Value = DateTime.Now;
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Text.RegularExpressions;
namespace WinUI
{
/// <summary>
/// 用于处理DBNull问题的DateTimePicker
/// </summary>
public class MDateTimePicker :DateTimePicker
{
private object dateValue;
public new string Text
{
get
{
return this.dateValue.ToString();
}
set
{
this.dateValue = value;
}
}
public object Value
{
get
{
if (dateValue==null)
{
Format = DateTimePickerFormat.Custom; this.CustomFormat = " ";
}
else
{
this.Format = DateTimePickerFormat.Short;
}
return dateValue;
}
set
{
dateValue = value;
base.Value = Convert.ToDateTime(value);
}
}
protected override void OnTextChanged(EventArgs e)
{
//base.OnTextChanged(e);
//if (Convert.ToDateTime(Value) == MaxDate)
//{
// Format = DateTimePickerFormat.Custom;
// CustomFormat = " ";
//}
//else
//{
// Format = DateTimePickerFormat.Long;
//}
}
protected override void OnClick(EventArgs e)
{
//base.OnClick(e);
//onEdit();
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (e.KeyCode == Keys.Delete)
{
this.Value = null;
}
else
{
//onEdit();
}
}
private void onEdit()
{
Format = DateTimePickerFormat.Long;
Value = DateTime.Now;
}
}
}