zoukankan      html  css  js  c++  java
  • 封装DateTimePicker并使用绑定时遇到的问题

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

            
        }

    }



     

    ---------------------------------------------------------------------
    每个人都是一座山.世上最难攀越的山,其实是自己.往上走,即便一小步,也有新高度
    .

    --做最好的自己,我能!!!

  • 相关阅读:
    Spring IOC之容器概述
    SQL Server之记录筛选(top、ties、offset)汇总
    [译]Java 设计模式之单例
    [译]Java 设计模式之适配器
    [译]Java 设计模式之桥接
    [译]Java 设计模式之装饰器
    [译]Java 设计模式 之模板方法
    [译]Java 设计模式之抽象工厂
    [译]Java 设计模式之工厂
    传入两个字符串,确认其中一个字符串重新排序后能否变为另一个字符串(也就是两个字符串相等)
  • 原文地址:https://www.cnblogs.com/tonyepaper/p/1202308.html
Copyright © 2011-2022 走看看