Marc Vitalis
Marc Vitalis

Reputation: 2249

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

Upvotes: 2

Views: 11704

Answers (3)

Sergio Ross
Sergio Ross

Reputation: 71

  1. Remember the dtp.Value in the dtp.DropDown event
  2. On Close-Up event compare dtp.Value with remembered. If it is the same, user didn't change value (= didn't click).

Upvotes: 0

Karthikeyan
Karthikeyan

Reputation: 11

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);
}

Upvotes: 1

Nerevar
Nerevar

Reputation: 156

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

Upvotes: 1

Related Questions