What event is raised when a user interacts with the DateTimePicker control?

◇◆丶佛笑我妖孽 提交于 2019-12-10 14:14:50

问题


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?


回答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
    }



回答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;
}



回答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.




回答4:


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


来源:https://stackoverflow.com/questions/9780800/what-event-is-raised-when-a-user-interacts-with-the-datetimepicker-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!