What event is raised when a user interacts with the DateTimePicker control?
Solution 1
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
}
Solution 2
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;
}
Solution 3
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.
user1157690
Updated on July 09, 2022Comments
-
user1157690 almost 2 years
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?