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

StackOverflow https://stackoverflow.com/questions/1426265

  •  07-07-2019
  •  | 
  •  

Question

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 :)

Was it helpful?

Solution

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 ...
}

OTHER TIPS

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top