I'm new to c#, in my program im using DateTimePicker Value changed event but i found ValueChanged event occurs when the user clicks on arrow or if the value is changed programatically as well, I want to identify only the user interacts of the DateTimePicker (not when the value is changed programatically), Is there any way to do this?
-
Question is unclear. Can you expand or correct your sentences so we get a clear idea of what you want? – Mathieu Mar 20 '12 at 03:00
-
Look for a `Click` event... This is a weird thing to want to do, though. – Cody Gray - on strike Mar 20 '12 at 03:23
-
I am facing the same problem, but if you create a demo winforms app solely with a single datetimepicker, and click in the arrow/calendar icon, the event is not fired. It is fired only when a different date is changed, as expected. So the problem must be we are setting the value manually somewhere in code, although I have not been able to find yet. – Veverke Jan 10 '18 at 12:05
-
I could not exactly understand what my problem was, but it was related to setting the MaxDate property of both my Start and End date parameters. I am not sure why was this happening, since DateTime is a Struct type, but my the datetimepicker.Value is an object reference. As far as I could get... giving up setting the MaxDate properties made the issue disappear. – Veverke Jan 10 '18 at 13:01
-
You could check for the Focus property. If it is false and the value is changed, that means that the Value is changed programmatically. – Sergey Oct 08 '19 at 08:05
4 Answers
You can turn off the event handler on the DropDown
event, and turn it back on when the drop down calender is closed by the user in the CloseUp
event:
private void dateTimePicker1_ValueChanged(object sender, EventArgs e) {
this.Text = dateTimePicker1.Value.ToString();
}
private void dateTimePicker1_DropDown(object sender, EventArgs e) {
dateTimePicker1.ValueChanged -= dateTimePicker1_ValueChanged;
}
private void dateTimePicker1_CloseUp(object sender, EventArgs e) {
dateTimePicker1.ValueChanged += dateTimePicker1_ValueChanged;
dateTimePicker1_ValueChanged(sender, e);
}
This prevents the ValueChanged
event from firing while the user is scrolling through the calendar during the drop down.
To change the date programmatically without firing the event, use the same concept:
private void ProgramChangesDateTime(DateTime dt) {
dateTimePicker1.ValueChanged -= dateTimePicker1_ValueChanged;
dateTimePicker1.Value = dt;
dateTimePicker1.ValueChanged += dateTimePicker1_ValueChanged;
}

- 80,625
- 14
- 153
- 225
Yes, take a look at the MSDN documentation. Especially, the OnValueChanged
event
You will need to wire your control up using this event:
In a constructor method:
dateTimePickerControl.ValueChanged += new EventHandler(picker_ValueChanged);
And here is the method signature:
void f_ValueChanged(object sender, EventArgs e)
{
//Do whatever you need when the value changes here
}
You can also do this from the designer. If you go to the Properties, then Events portion, it lists all of the events. Just double click and it will create the method signature and wiring for you.
UPDATE TO YOUR UPDATE
If you specifically want to check whether this is a programmatic change or not, then you want to do something like this:
Create a global variable in your class
Boolean isProgrammaticEvent = false
;
Before your programmatic change:
isProgrammaticEvent = true;
//Change picker value
In your event wiring:
void f_ValueChanged(object sender, EventArgs e)
{
Boolean isThisProgrammatic = isProgrammaticEvent;
isProgrammaticEvent = false;
if(isThisProgrammatic)
return;
//Do whatever you need when the value changes here
}

- 66,056
- 18
- 147
- 180
-
Justin thanks for the answer but this event evokes in both of the situations, (when user changes or value changed programatically ) is there any way to identify solely when user changes values of the DateTimePicker – user1157690 Mar 20 '12 at 03:09
-
I do not believe there is a method, but you can have a global boolean that you can turn on before you programmatically change, and then have the event always turn it off? Globals are not great programming practice, so make sure to think through that you really need this functionality :) Here is the SO question already asked along those lines: http://stackoverflow.com/questions/2806601/how-to-distinguish-user-vs-programmatic-changes-in-winforms-checkbox – Justin Pihony Mar 20 '12 at 03:16
-
I have updated the code to reflect what would need to be done BTW – Justin Pihony Mar 20 '12 at 03:27
-
global variable is bad for multithreading: what if change event happends simultaniously 2 times: programmatic and manual – obratim Apr 02 '21 at 11:55
You can derive from DateTimePicker
to know when the user has made the change:
class DateTimePickerUser : DateTimePicker
{
private bool userSetValue;
public bool UserSetValue
{
get
{
return userSetValue;
}
}
public DateTimePickerUser()
{
userSetValue = true;
}
public new DateTime Value
{
get
{
return base.Value;
}
set
{
userSetValue = false;
base.Value = value;
userSetValue = true;
}
}
}
When you use the DateTimePickerUser
on a Form
you just check the flag in the ValueChanged event:
private void dateTimePickerUser1_ValueChanged(object sender, EventArgs e)
{
if (dateTimePickerUser1.UserSetValue)
this.Text = "User changed value.";
else
this.Text = "Code changed the value.";
}
This is similar to Justin Pihony's example, but you do not need to set the flag yourself, just rely on this control to do it.

- 1,830
- 1
- 22
- 33
You could check the Focused property of the DateTimePicker control. If it is false, that probably means that the value is changed programmatically. Additionally, you could check if the calendar popup is currently open.
The only exception is the case where the user is entering in the date-time picker.
public class MyDateTimePicker : DateTimePicker
{
/// <summary>
/// Occurs when the property Value is changed programmatically.
/// </summary>
public event EventHandler<EventArgs> ValueChangedProgrammatically;
/// <summary>
/// Gets or sets a boolean that indicates if the calendar popup is open.
/// </summary>
public bool IsOpen { get; private set; }
/// <summary>
/// Raises the ValueChangedProgrammatically event.
/// </summary>
/// <param name="e">Event arguments.</param>
protected virtual void OnValueChangedProgrammatically(EventArgs e)
{
ValueChangedProgrammatically?.Invoke(this, e);
}
/// <summary>
/// Raises the DropDown event.
/// </summary>
/// <param name="e">Event arguments.</param>
protected override void OnDropDown(EventArgs e)
{
base.OnDropDown(e);
IsOpen = true;
}
protected override void OnCloseUp(EventArgs e)
{
base.OnCloseUp(e);
IsOpen = false;
}
/// <summary>
/// Raises the ValueChanged event.
/// </summary>
/// <param name="e">Event arguments.</param>
protected override void OnValueChanged(EventArgs e)
{
base.OnValueChanged(e);
if (!Focused && !IsOpen)
{
/* If the control has no focus and the calendar is not opened, the value is most likely changed programmatically. */
OnValueChangedProgrammatically(e);
}
}
}

- 581
- 1
- 5
- 12