自定义日期时间选择器控件

本文关键字:控件 选择器 时间 日期 自定义 | 更新日期: 2023-09-27 18:05:19

我已经创建了实现DateTimePicker的用户控件。但是我对Databindings有问题。当控件加载dtp时。值有默认的DateTime . now值。为什么?

//Part of Form InitializeComponent() : 
this.dtp_user.DataBindings.Add(new System.Windows.Forms.Binding("Value",           this.employeesBindingSource, "BirthDate", true)); 
// Custom Control Contains two buttons and textBox (usrText)
public partial class usrDateTimePicker : UserControl
{
    private DateTimePicker dtp;
    private bool _cheked;
    public bool Checked
    { 
        get { return _cheked; }
        set { _cheked = value; }
    }
    public DateTime Value
    { 
        get { return dtp.Value; }
        set
        {
            if (value < DateTime.MinValue && value > DateTime.MaxValue)
                value = DateTime.Now;
            dtp.Value = value;
        }
    }
    public override string Text
    {
        get { return usrText.Text; }
    }
    public ControlBindingsCollection DataBindings
    {
        get { return dtp.DataBindings;}
    }
    usrCalendar clnd;
    Popup _popup;
    public usrDateTimePicker()
    {
        InitializeComponent();
        InitStyles();
        InitControls();
    }
    private void InitStyles()
    {
        //
    }
    private void InitControls()
    {
        dtp = new DateTimePicker();
        clnd = new usrCalendar();
        _popup = new Popup(clnd);
        _popup.Closed += popup_Closed;
    }
    protected override void OnLoad(EventArgs e)
    {
        usrText.Text = FormatDate(dtp.Value);
        base.OnLoad(e);
    }
    protected override void OnSizeChanged(EventArgs e)
    {
        //
    }
    private void btn_datetimepick_Click(object sender, EventArgs e)
    {
        if (!_popup.Visible)
        {
            _popup.Show(this);
        }
        else
            _popup.Close();
    }
    private void popup_Closed(object sender, ToolStripDropDownClosedEventArgs e)
    {
        dtp.Value = clnd.Value;
        usrText.Text = FormatDate(dtp.Value);
        _cheked = true;
        if (usrText.Text == string.Empty || usrText.Text == "")
            _cheked = false;
    }
    private string FormatDate(DateTime date)
    {
        // 
    }

自定义日期时间选择器控件

默认情况下,DatePicker控件的Value被设置为DateTime.Now,除非您特别设置一个值。在这里阅读