Click here to Skip to main content
15,908,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I use the DateTimePicker Enter Event to set the Max Date to Yesterday's Date as I do not control if the user decides to leave the application open for days.

I also use the DateTimePicker ValueChanged Event to adjust my dialogues based on those selected dates.

The problem that I am facing is that every time I adjust the Max Date through the Enter Event, ValueChanged Event is also called and the value taken from the DateTimePicker is not the user value. Is there anything that I can do to only listen for the input change from the user?

What I have tried:

The DateTimePicker CloseUp seems to help but only if the user uses the calendar to pick the date and not manually typing it in.
Posted
Updated 25-Jun-19 2:59am

1 solution

I use a Boolean "flag" to control this.

A private property on the form e.g.
C#
bool codeUpdate;
When I need to programmatically change something I first set the boolean to true, do my changes, then set it back to false e.g.
C#
codeUpdate = true;
//make my changes
//
codeUpdate = false;
Then in each of the control's events that I want to not fire if I'm doing it in code I have a single line
C#
if (codeUpdate) return;
This is very old-school by the way!
A more modern approach might be to actually unhook the event(s) entirely while you make your programmatic changes e.g.
C#
DatePicker1.ValueChanged -= DatePicker1_ValueChanged);
// make my changes
//
DatePicker1.ValueChanged += DatePicker1_ValueChanged;
I prefer the first approach - to me it's clearer what's going on, especially if you use an appropriately named variable. I'm sure someone will put forward an alternative argument though.

To be honest, I probably wouldn't keep changing the Max Date through the Enter event. I would just validate the DatePicker value against "yesterday" in the ValueChanged event then this problem just goes away
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900