用于处理DBNull问题的DateTimePicker

08/5/3改进版,请大家给些建议.谢谢!
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
    
{
        
public object Value
        
{
            
get
            
{
                
return base.Value;
            }

            
set
            
{
                
try
                
{
                    DateTime dt
=Convert.ToDateTime(value);
                    
base.Value = dt;
                }

                
catch (Exception ex)
                
{
                    
base.Value = MaxDate;
;
                }

            }

        }

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

            
else
            
{
                onEdit();
            }

        }

        
private void onEdit()
        
{
            Format 
= DateTimePickerFormat.Long;
            Value 
= DateTime.Now;
        }

        
    }

}


posted @ 2008-05-03 10:19  武广敬  阅读(441)  评论(2编辑  收藏  举报