Menu

#610 TCalendarEdit text gets cleared when control loses focus

8
pending
OWLExt (15)
1
2025-05-19
2025-05-18
No

When the calendar popup is opened, and a date selected, its string representation gets set in the edit box.
But when the popup is opened again or the user clicks outside of the control, it tries to parse the date and fails and empties the edit box.

Discussion

  • Ognyan Chernokozhev

    The problem is that TDate::AsString() by default uses the "mmm dd,yy" format for printing the date, the parsing is done using the TDate(LPCTSTR) constructor, which internally calls TDate::ParseFrom(LPCTSTR str, LPCTSTR format), and its implementation does not support the "mmm dd,yy" format.

    Two possible quick fixes for the TCalendarEdit class:

    1. Use TDate::SetPrintOption(TDate::WinIntSection) to set the format for TDate::AsString() when setting the text in the edit control. The problem is that first of all, this changes the behavior of the control, as the date is now printed in a different format, and second - the SetPrintOption method is static and affects all instances of the TDate class. Even if restoring the old value immediately after printing the date, there is a small risk that other threads may be affected.
    2. Use instead the TDate(const tistream& s); constructor that supports "mmm dd,yy" format.

    It is possible also to change the TDate(LPCTSTR) constructor to internally create a stream and use the stream parsing, but that may affect existing code.

    The best long-term solution may be to ditch the usage of TDate in TCalendarEdit and refactor it to make use of the standard chrono library.

     
    👍
    1
  • Ognyan Chernokozhev

    • status: open --> pending
     
  • Ognyan Chernokozhev

    The second proposed fix implemented in [r7859].

     

    Related

    Commit: [r7859]


Log in to post a comment.