1

I am trying to get the previous value of DateTimePicker when it hits the event ValueChanged. The other possible solution to my problem would be getting whether the user clicked on a value and chose it or it was invoked by some method. My problem is I need to know what caused this event and execute some code only if the previous value was different. I have read this and didn't like the solution to the possible way #2.

So again:

if user clicks
{
    execute some code
}
else // if method was invoked
{
    do NOT execute
}

OR

if value is NOT the same as previously
{
    execute some code
}
else
{
    do NOT execute
}

Either of that suits me, but I am unable to find the previous value in the list of available properties nor in EventArgs (which is null :( ). Hope I was clear what I want to achieve. If you ask for the reasons that I need this, it is irrelevant and I cannot edit the other code, just this method.

Community
  • 1
  • 1
Andrius Naruševičius
  • 8,348
  • 7
  • 49
  • 78

2 Answers2

2

The ValueChanged-Event, as the name implies, will only be fired when the Value of the DateTimePicker changes. You do not have to check if the value has changed in your code.

You are stating that you EventArgs is null, but it should be EventArgs.Empty, when used in an unmodified framework.

If you want to do something else with the LastValue you can use a customized DateTimePicker like this.

public class LastDateTimePicker : DateTimePicker {
    protected override void OnValueChanged(EventArgs eventargs) {
        base.OnValueChanged(eventargs);

        LastValue = Value;
        IsProgrammaticChange = false;
    }

    public DateTime? LastValue { get; private set; }
    public bool IsProgrammaticChange { get; private set; }

    public new DateTime Value { 
        get { return base.Value; }
        set {
            IsProgrammaticChange = true;
            base.Value = value;
        }
    }
}

EDIT I have changed my example to met your requirements of checking programmatic changes, as stated in your comment.

dwonisch
  • 5,595
  • 2
  • 30
  • 43
  • That is true. It is empty :) But anyways, that doesn't help :( – Andrius Naruševičius Oct 05 '12 at 07:43
  • Then you have to respecify your question. You want the check if value really changes before the ValueChanged is processed, but this check is already done by the DateTimePicker before the ValueChanged is fired. – dwonisch Oct 05 '12 at 07:53
  • Maybe then I am just using the wrong method? As soon as the user chooses the date and it is a valid input, I want to check if it was really changed OR it was the user that made it change and not some other method. – Andrius Naruševičius Oct 05 '12 at 08:00
  • Then you have to set programmatic changes in another way [What event is raised when a user interacts with the DateTimePicker control](http://stackoverflow.com/questions/9780800/what-event-is-raised-when-a-user-interacts-with-the-datetimepicker-control). – dwonisch Oct 05 '12 at 08:04
  • I have edited my example, so that it meets your requirements. On value changed you can check whether IsProgrammaticChange is true or not. – dwonisch Oct 05 '12 at 08:12
  • Thank you very much for your effort, that seems to have solved my issue :) – Andrius Naruševičius Oct 05 '12 at 08:42
0

The ValueChanged event is fired post validation, after the value has changed. You can't get the value before the change from this event.


If you want to extend the validation of the control then you could use the Validating event.


If you just want to trigger some code after the change then you could write code to store the previous value, somthing like this.

private DateTime oldValue = SomeDateTimePicker.Value;

private SomeDateTimePickerValueChanged(object sender, EventArgs e)
{
    if (SomeDateTimePicker.Value != oldValue)
    {
        //Do Something
    }

    this.oldValue = SomeDateTimePicker.Value
}
Jodrell
  • 34,946
  • 5
  • 87
  • 124