请教各位高手:
我想把一个控件封装起来.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;
        }
        
    }
}

解决方案 »

  1.   

    点击日期后,设置的是DateTimePicker控件自身的Value(base.Value),根本不可能执行你的代码。
    你要另外放一个文本框(TextBox或MaskEditBox)在该控件上来显示日期,屏蔽原来显示日期的文本框。
      

  2.   

    上面的思路就可以了
     做一个用户控件.用户控件放一个TextBox就可以了 如果Value为Null的时候就可以了把Textbox显示出来 
    绑定的时候绑定到Text就可以了 
    value在适当的时候显示出来就可以了
      

  3.   

    但是他一直只使用我这个类Value的Set方法,不使用我这个类的Value的Get方法,还是调动Base.Value.不知道为什么啊
    我试了一下,get和set都可以的,设置null给它,它也会返null给我。只是显示问题,你得onPain一下。
      

  4.   

    http://blog.csdn.net/csharp_start/archive/2007/11/26/1902897.aspx
      

  5.   

    试验如下:
    1. 自定义控件继承自DateTimePicker;
    2. 属性:        object theValue = null;
            public new object Value
            {
                get { return theValue; }
                set { theValue = value; }
            }3. 建立测试用的数据库[test],表[table_a],字段[id],时间字段[dt](可空)
    4. 写入一条记录,时间为2008-8-8
    5. 绑定:this.myDateTimePicker1.DataBindings.Add("Value", ds1.table_a, "dt");6. 查询后显示:MessageBox.Show(this.myDateTimePicker1.Value + "");  //弹出“2008-8-8 0:00:00”
    7. 表里: Ctrl+0 把时间设置null
    8. 再查询后显示:MessageBox.Show(this.myDateTimePicker1.Value + "");  //弹出“”
    value获取没问题,只是显示在控件上的时间,如果value=null时,你要onpain一下把它擦掉。
      

  6.   

    感谢各位的回复
    不过我测试还是不会使用GET方法.大家可不可以帮我看一下..我的代码哪里有问题.谢谢!
      

  7.   

    new public object Value 这样不知道能不能解决问题