DateTimeUpDown.cs
Method: TryParseDateTime
This method can result in a DateTime being incorrectly parsed from the display text when a string representing a time is passed to the method.
This method correctly determines the time from the string but does not preserve the day, month and year of the selected date, instead defaulting to the day month and year the time was parsed.
This is an issue if the DateTime value linked to the TimePicker is used elsewhere to store day, month and year information.
One possible solution is to expose a new property IsTimePicker to the DateTimeUpDown control so that it is possible to determine when to preserve the day, month and year information in the current value.
One would expect a similar issue when the selected day month and year are changed in the DateTimePicker. However DateTimePicker.Calendar_SelectedDatesChanged correctly preserves the all other parts of the date, only altering the day month and year.
A possible workaround is:
```
bool isValid;
var current = this.Value.HasValue ? this.Value.Value : DateTime.Parse(DateTime.Now.ToString(), this.CultureInfo.DateTimeFormat);
isValid = DateTimeParser.TryParse(text, this.GetFormatString(Format), current, this.CultureInfo, out result);
if (!isValid)
{
isValid = DateTime.TryParseExact(text, this.GetFormatString(this.Format), this.CultureInfo.DateTimeFormat, DateTimeStyles.None, out result);
if (isValid && IsTimePicker)
{
// If the text is a time and this is a time picker, we must preserve the current day, month and year selection.
result = new DateTime(current.Year, current.Month, current.Day, result.Hour, result.Minute, result.Second);
}
}
return isValid;
```
Comments: Fixed in v2.1.
Method: TryParseDateTime
This method can result in a DateTime being incorrectly parsed from the display text when a string representing a time is passed to the method.
This method correctly determines the time from the string but does not preserve the day, month and year of the selected date, instead defaulting to the day month and year the time was parsed.
This is an issue if the DateTime value linked to the TimePicker is used elsewhere to store day, month and year information.
One possible solution is to expose a new property IsTimePicker to the DateTimeUpDown control so that it is possible to determine when to preserve the day, month and year information in the current value.
One would expect a similar issue when the selected day month and year are changed in the DateTimePicker. However DateTimePicker.Calendar_SelectedDatesChanged correctly preserves the all other parts of the date, only altering the day month and year.
A possible workaround is:
```
bool isValid;
var current = this.Value.HasValue ? this.Value.Value : DateTime.Parse(DateTime.Now.ToString(), this.CultureInfo.DateTimeFormat);
isValid = DateTimeParser.TryParse(text, this.GetFormatString(Format), current, this.CultureInfo, out result);
if (!isValid)
{
isValid = DateTime.TryParseExact(text, this.GetFormatString(this.Format), this.CultureInfo.DateTimeFormat, DateTimeStyles.None, out result);
if (isValid && IsTimePicker)
{
// If the text is a time and this is a time picker, we must preserve the current day, month and year selection.
result = new DateTime(current.Year, current.Month, current.Day, result.Hour, result.Minute, result.Second);
}
}
return isValid;
```
Comments: Fixed in v2.1.