繁体   English   中英

DateTimePicker:如果用户在弹出式日历中单击/选择日期,该如何处理?

[英]DateTimePicker: How will I handle if the user clicked/selected date in the popup calendar or not?

我注意到C#的DateTimePicker在更改月份或年份时会自动更改日期。 因此,我要做的是重写ValueChanged,并且仅在引发CloseUp事件时才触发它。

现在的问题是,如果用户实际上在日历中选择了日期,或者用户在控件外部单击,则需要一个事件。

你能帮我吗? 谢谢 :)

在派生类中,重写的OnValueChanged方法很适合我。

public class MyDateTimePicker : DateTimePicker
{
    // ... some stuff

    protected override void OnValueChanged(EventArgs eventargs)
    {
        System.Diagnostics.Debug.Write("Clicked -  ");
        System.Diagnostics.Debug.WriteLine(this.Value);
        base.OnValueChanged(eventargs);
    }

    protected override void OnCloseUp(EventArgs eventargs)
    {
        System.Diagnostics.Debug.Write("Closed -  ");
        System.Diagnostics.Debug.WriteLine(this.Value);
        base.OnCloseUp(eventargs);
    }

    // some more stuff ...
}

在派生类中,重写的OnCloseUp方法起作用是因为在重置日期时间控件时,不会触发ValueChange事件,因此CloseUp事件派上了用场。

void ProductionDateSchdl_CloseUp(object sender, EventArgs e)
{
    this.ProductionDateSchdl.CustomFormat = "MM/dd/yyyy";
    ProductionDate = Convert.ToDateTime(this.ProductionDateSchdl.Value);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM