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

            
        }

    }



     

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

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

  • 相关阅读:
    开源:不断创新的动力
    Inkpad中文翻译已合并到官方项目
    Inkpad绘图原理浅析
    Vectoroid
    发布大幅重构优化的 TouchVG 1.0.2
    清理掉一直想研究的开源项目
    函数指针调用方式
    音视频直播优化
    std::unique_lock与std::lock_guard区别示例
    c++容器的操作方法总结
  • 原文地址:https://www.cnblogs.com/tonyepaper/p/1202308.html
Copyright © 2011-2022 走看看