DateTimePicker ValueChanged event fires twice
-
Hi,i have a DateTimePicker DateTimePicker1 in my Form,when user enters a future date a message "Date cannot be in the future" displays in my DateTimePicker ValueChanged event i have this code
TimeSpan ts = (DateTime)DateTimePicker1.Value - DateTime.Now; if (ts.Milliseconds > 0) { MessageBox.Show"Date cannot be in the future".); DateTimePicker1.Value = DateTime.Now; return; }
The message Displays twice,which makes me think the event fires twice. how can i make it fire just once? or is there something i am mssing? thanks:) regards paula -
Hi,i have a DateTimePicker DateTimePicker1 in my Form,when user enters a future date a message "Date cannot be in the future" displays in my DateTimePicker ValueChanged event i have this code
TimeSpan ts = (DateTime)DateTimePicker1.Value - DateTime.Now; if (ts.Milliseconds > 0) { MessageBox.Show"Date cannot be in the future".); DateTimePicker1.Value = DateTime.Now; return; }
The message Displays twice,which makes me think the event fires twice. how can i make it fire just once? or is there something i am mssing? thanks:) regards paulaHello I can think of another way to do the job. Why don't you change the MaxDate property? something like:
DateTimePicker1.MaxDate = DateTime.Now;
It'll work even without messages -sometimes they are annoying to beginner users- If you insist on showing a MessageBox, just ignore the first time:), maybe:bool SecondTime = false; void MyEventHandler(params) { TimeSpan ts = (DateTime)DateTimePicker1.Value - DateTime.Now; if (ts.Milliseconds > 0 && SecondTime) { MessageBox.Show"Date cannot be in the future".); DateTimePicker1.Value = DateTime.Now; SecondTime = false; return; } SecondTime = true; }
-
Hi,i have a DateTimePicker DateTimePicker1 in my Form,when user enters a future date a message "Date cannot be in the future" displays in my DateTimePicker ValueChanged event i have this code
TimeSpan ts = (DateTime)DateTimePicker1.Value - DateTime.Now; if (ts.Milliseconds > 0) { MessageBox.Show"Date cannot be in the future".); DateTimePicker1.Value = DateTime.Now; return; }
The message Displays twice,which makes me think the event fires twice. how can i make it fire just once? or is there something i am mssing? thanks:) regards paulaPaula, The reason it is firing twice is because you are changing it twice; once when you select on the form and another with your line of code just before your return where you change it back to the current date. HTH
-
Hi,i have a DateTimePicker DateTimePicker1 in my Form,when user enters a future date a message "Date cannot be in the future" displays in my DateTimePicker ValueChanged event i have this code
TimeSpan ts = (DateTime)DateTimePicker1.Value - DateTime.Now; if (ts.Milliseconds > 0) { MessageBox.Show"Date cannot be in the future".); DateTimePicker1.Value = DateTime.Now; return; }
The message Displays twice,which makes me think the event fires twice. how can i make it fire just once? or is there something i am mssing? thanks:) regards paulaThe one time I experienced something like that, I found that I had done a subscription to the event twice for that event handler. So do a search in your code for += and make sure you haven't done that.