Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 322
user1157690 Avatar asked Mar 20 '12 02:03

user1157690


People also ask

What is the event when a value is selected for a DateTimePicker?

ValueChanged event is fired when the value of a DateTimePicker control is changed.

What is DateTimePicker control?

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.

What does the DateTimePicker allow for?

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.


2 Answers

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;
}
like image 73
LarsTech Avatar answered Sep 29 '22 04:09

LarsTech


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
    }
like image 28
Justin Pihony Avatar answered Sep 29 '22 04:09

Justin Pihony