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?
ValueChanged event is fired when the value of a DateTimePicker control is changed.
A date and time picker (DTP) control provides a simple and intuitive interface through which to exchange date and time information with a user. For example, with a DTP control you can ask the user to enter a date and then easily retrieve the selection.
The DateTimePicker control is used to allow the user to select a date and time, and to display that date and time in the specified format.
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;
}
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With