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;
            }

            
        }

    }



     

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

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

  • 相关阅读:
    Java JMX 监管
    Spring Boot REST(一)核心接口
    JSR 规范目录
    【平衡树】宠物收养所 HNOI 2004
    【树型DP】叶子的颜色 OUROJ 1698
    【匈牙利匹配】无题II HDU2236
    【贪心】Communication System POJ 1018
    【贪心】Moving Tables POJ 1083
    Calling Extraterrestrial Intelligence Again POJ 1411
    【贪心】Allowance POJ 3040
  • 原文地址:https://www.cnblogs.com/tonyepaper/p/1202308.html
Copyright © 2011-2022 走看看