Question Question on DateTimePicker

priyamtheone

Well-known member
Joined
Sep 20, 2007
Messages
96
Programming Experience
Beginner
How can i trap the checked/unchecked state of a datetimepicker through an event. I'm trying to execute two different methods based on the checked/unchecked state of a datetimepicker when the user clicks the datetimepicker. When the user checks the dtPicker Method A is executed, when the user unchecks it Method B is executed. How can i trap it.
Regards.
 
You can get to the properties of the datetimepicker, and should be able to use the Checked property to determine whether it has been checked.

VB.NET:
If  DateTimePicker1.Checked Then
     'Method A
Else
     'Method B
End If
 
It really is stupid that the DTP doesn't have a CheckedChanged event. If I remember correctly you will have to use the ValueChanged event, which is raised when the control is checked or unchecked, and then compare the current Checked value with the old value that you stored away yourself to know whether the event is due to a check or not.
 
Question on DateTimePicker.

jmcilhinney, are you sure the datetimepicker fires the ValueChanged event when the checkbox is checked/unchecked while the value is not changed? B'coz as i have seen in my machine (I'm working on .Net2005) that event is not fired in that way. If it works then either i am missing something or that feature is not supported in 2005.
If it doesn't work then what if i don't change the value of datetimepicker and only check/uncheck it? Then my methods wont be executed. What I wanna know is whether there is an event that's fired when the checkbox is checked/unchecked irrespective of value change. I managed to solve this
problem a bit by using DateTimePicker_MouseUp event but yet the problem persists if the user uses the keyboard instead of the mouse to check/uncheck the control. Plz refer further. Regards.
 
jmcilhinney is right, the ValueChanged event is probably the best to use. I tested the Leave, and Validating events also and if you change the day value to a single digit number (6) then the order of the events are Leave, Validating, ValueChanged. Otherwise as soon as the second digit for the day is entered it fires the ValueChanged event.
 
jmcilhinney, are you sure the datetimepicker fires the ValueChanged event when the checkbox is checked/unchecked while the value is not changed?
I just noticed that you're using .NET 1.0. I think that maybe it doesn't in .NET 1.x but it does from .NET 2.0 onwards. Is there any possibility of your upgrading to VB 2008 Express? It's free and you'll find the IDE, the Framework and the language vastly improved.
 
Question on Datetimepicker

It really is stupid that the DTP doesn't have a CheckedChanged event. If I remember correctly you will have to use the ValueChanged event, which is raised when the control is checked or unchecked, and then compare the current Checked value with the old value that you stored away yourself to know whether the event is due to a check or not.

I have checked Datetimepicker_valuechanged event. U r right, it is fired everytime the checkbox is checked/unchecked. But a new problem arises from here. Here is the concept I'm trying to work on. Suppose u hav a dtpicker and a listbox on ur form. The listbox contains a list of dates (in dd MMM yyyy format). When u select a date from lstBox it sets that date as the value of the dtPicker. When u uncheck the dtPicker (while not changing its value) it'll set the selectedindex of listbox to -1.
But the problem is, for the 1st time when u select a date from lstbox, the date is set in dtpicker and its checkbox is checked. Right next try to uncheck the dtPicker (without changing its value) u see it's not firing the ValueChanged event and the selectedindex of lstBox is not setting to -1. Plz refer.
N.B.: I'm working on .Net framework 2.0
 
Back
Top