简体   繁体   English

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

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

I noticed that C#'s DateTimePicker is changing the date automatically when I'm changing Month or Year. 我注意到C#的DateTimePicker在更改月份或年份时会自动更改日期。 So what I did is to override the ValueChanged and it is only fired when the CloseUp event is raised. 因此,我要做的是重写ValueChanged,并且仅在引发CloseUp事件时才触发它。

The problem now is I need an event if the user actually selected date in the calendar or the user clicks outside of the control. 现在的问题是,如果用户实际上在日历中选择了日期,或者用户在控件外部单击,则需要一个事件。

Can you help me with this? 你能帮我吗? Thanks :) 谢谢 :)

In a derived class, the overriden OnValueChanged method works for me well. 在派生类中,重写的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 ...
}

In a derived class, the overriden OnCloseUp method works because when you reset the datetime control, the ValueChange event does not fire, so the CloseUp event comes in handy. 在派生类中,重写的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.

相关问题 如何从日历的文本框中(从Icon弹出窗口)获取选定的日期? - How to get selected date into the text box from the calendar-that popup from Icon? 单击时 Bootstrap DateTimePicker 不显示日历 - Bootstrap DateTimePicker not showing calendar when clicked 当用户单击DropDown按钮时,如何不在DateTimePicker中显示日历 - How not to show calendar in the DateTimePicker when user clicks DropDown button 如果未选择日期,如何设置datetimepicker为空值(c#winforms) - how to set datetimepicker with null value if date not selected(c# winforms) DateTimePicker日历默认为上个月/年份 - DateTimePicker Calendar defaults to last Month/Year selected 每当用户选择日期时,如何在DateTimePicker中设置默认小时? - How can I set a default hour in the DateTimePicker every time the user selects a date? 如何在日历中捕获所选日期更改事件? - How to catch the selected date changed event in calendar? 如何在asp:Calendar中选择“选定日期”? - How to select the “selected Date” in asp:Calendar? 在其他datetimepicker winforms C#中,选择“日期”打开datetimepicker日历 - Open the calendar of datetimepicker on select of Date in other datetimepicker winforms C# 如何在DateTimePicker控件中自定义日历? - How to customize Calendar in DateTimePicker Control?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM