What event occurs when a user interacts with a DateTimePicker control?

I'm new to C # in my program im using the DateTimePicker Value change event, but I found that the ValueChanged event occurs when the user clicks on the arrow or, if the value changes also programmatically, I want to identify only DateTimePicker user interactions (not when the value changes programmatically) is there a way to do this?

+5
source share
4 answers

Yes, see the MSDN documentation . In particular, the OnValueChanged event

You will need to connect your control using this event:

In the 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" section, it lists all the events. Just double click and it will create the signature and posting method for you.

UPDATE TO YOUR UPDATE

If you specifically want to check if this is a software change or not, then you want to do something like this:

Create a global variable in your class

Boolean isProgrammaticEvent = false ;

Before making changes to the program:

  isProgrammaticEvent = true;  //Change picker value 

In the event posting:

  void f_ValueChanged(object sender, EventArgs e)  {    Boolean isThisProgrammatic = isProgrammaticEvent;    isProgrammaticEvent = false;    if(isThisProgrammatic)      return;    //Do whatever you need when the value changes here  } 
+5
source

You can turn off the event handler in the DropDown event and enable it again when the custom frame with the drop-down calendar is closed 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 ValueChanged while the user scrolls the calendar during the drop-down list.

To change the date programmatically without triggering an event, use the same concept:

 private void ProgramChangesDateTime(DateTime dt) { dateTimePicker1.ValueChanged -= dateTimePicker1_ValueChanged; dateTimePicker1.Value = dt; dateTimePicker1.ValueChanged += dateTimePicker1_ValueChanged; } 
+9
source

You can get a DateTimePicker to find out when the user 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 DateTimePickerUser on a Form , you simply 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 Pihoniโ€™s example, but you donโ€™t need to set the flag yourself, just rely on this control to do this.

+1
source

You can check the Focused property of the DateTimePicker control. If the value is false, this probably means that the value is changing programmatically. In addition, you can check if the calendar popup is open.

The only exception is when the user enters data while selecting a date and time.

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

Source: https://habr.com/ru/post/1402355/


All Articles