DatePicker resets itself to the current date. How can I stop this from happening? I have already tried setting the date value to a certain date but, it still resets itself.
You tell us what you did but not how. How is what is important. The picker resets itself because it knows it is today’s date when you request it. You need to provide code to save the changed date (or the date you think it should display) and instantiate it.
SetDateToDisplay( year , month , day )
Allows the user to set the date to be displayed when the date picker opens. Valid values for the month field are 1-12 and 1-31 for the day field.
SetDateToDisplayFromInstant( instant )
Allows the user to set the date from the instant to be displayed when the date picker opens.
How you tried to do this, we haven’t a clue. You probably need these Blocks
Show what you did; then someone might be able to offer specific advice.
Otherwise follow the advice here: DatePicker using the methods and events that are available.
I have the same issue. I click on the datepicker, in the dialog I choose a date (other than today) and I click OK.
Then when I do that again and now I cancel the dialog, then the date gets reset to the current date.
So now when I click on the datepicker the date is the current date.
I don't want that, when I cancel it means I want to keep the date that was last set.
So for an unknown reason the datepicker is reset when I click cancel.
Store your last selected date to a variable ?
That won't work because there is no listener on the cancel button.
So there is no way I know I have to set my saved date in the datepicker.
After Cancel, you can verify the date picker's date and compare it to today's date (Clock Block). So then you will know.
Not possible because there is no way to detect 'cancel' is clicked.
Yes, but if the DatePicker is tapped, we know for sure that either the date has been changed, accepted or cancelled, so as I explained we can work from there. Use the .AfterDateSet Block.
DatePickerVal.aia (2.2 KB)
As you can see, it's possible to test. If the date is the current date and you want a different date to be the default, you can add code to do that. In this example, if the User does change the date, that will be the default when the picker is next used.
Thanks, I will try that.
This is a bug i think, AfterDateSet should not be called when canceled, or we need a separate OnCancel event.
It's not a bug, it's an unusual requirement that the AfterDateSet block can accommodate. If the User inputs a date (in the picker) but then hits Cancel, it's reasonable to assume he/she changed their mind - but it could have been accidental. Either way, if you need to control what date is displayed when the picker is next used, you can set it in code as I and Steve have shown.
I withdrawn what I said in post 12, since I tested on code.appinventor.mit.edu, it's working fine.
there is no "reset to current date" after click cancel.
however, if the SetDateToDisplayFromInstant is not called when TouchDown, when you clicked cancel, the DatePicker.Instant will reset to current date, but the DatePicker.Text will not change, unless you set it again.
That means, the AfterDateSet event is not raised when Cancel is clicked.
As you can see though, the block can still be used to ensure the App always 'knows' the date held by the picker.