简体   繁体   中英

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. So what I did is to override the ValueChanged and it is only fired when the CloseUp event is raised.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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