I would like to change the behaviour of the DateTimePicker control.
I have therefore inherited the WideJ control to then insert in the Validating event in order to complete the date with my own parser.
For example if I type in a number (e.g. 24) I want to set the date to “24/current month/current year”.
Unfortunately, however, the parser works but I cannot update the content with my recalculated value.
private void DgDate_Validating(object sender, System.ComponentModel.CancelEventArgs e) {
InvalidMessage = null;
string err;
((DgDate)sender).Value = ParseFromStr(((DgDate)sender).Text, out err);
if (err != null) {
InvalidMessage = err;
e.Cancel = true;
return;
}
}
Where ‘ParseFromStr’ is a method that returns a Valid Datetime.
If I type in the number 24 and press Tab or click on another control, despite the assignment (in red) in the Validating event,
the value 24 remains written in the control, if I return to the control and then press Tab again the value is displayed.
I have tried using all the update methods I could find (Update, Invalidate, Refresh), but nothing I can’t get the value to format correctly.
In Debug, the Value and text value of the control are correct, but still an incomplete date (24) is displayed and marked in red.
Could you suggest a solution or show me where I am going wrong?
Best Regards
Tiziano
Hi Tiziano,
You have to override the ParseEditText method of the DateTimePicker and implement your own parser there.
“24”, or any 2 digit number is not a valid Date string, that’s why it fails the first time but shows the last value the second time, because even though the string was invalid, the value was correctly set so the next time you write any other invalid string, the last valid value is going to show up!
by overriding the ParseEditText method, you can control how you parse your date, and even implement your own validation rules to be used in the Validating event.
I have attached a sample for you to check out.
HTH,
Alaa
Please login first to submit.