Click here to Skip to main content
Click here to Skip to main content

Editable Date Picker

, 24 Jan 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Fully editable date picker control based on CDateTimeCtrl

Introduction

Whilst I like to use the standard date picker control wherever the user should enter a date, I often find that I need it to be fully editable. The standard control has its own way of becoming editable, but this doesn't give me a plain edit control that I can do what I like with.

Documentation

The control is based on Microsoft's CDateTimeCtrl class. I have used a sneaky trick to allow me to completely replace the standard control with my own edit control and button, whilst still allowing the class to be derived from CDateTimeCtrl. I do this by destroying the existing window and then creating my own and attaching it to the class. See CDateTimeEditCtrl::OnRecreate for the workings.

Because of the way I have done this, use of the control is the same as using CDateTimeCtrl. You should add a date picker to your dialog resource as normal and then create a member variable for it of type CDateTimeEditCtrl. The usual date picker styles are supported, with the exception of DTS_UPDOWN, DTS_SHOWNONE, DTS_APPCANPARSE, DTS_LONGDATEFORMAT and DTS_TIMEFORMAT.

Messages sent to the control are interpreted and acted on in the same way as they would be for the standard control, with the exception of DTM_GETRANGE, DTM_SETRANGE and DTM_SETFORMAT, which are not supported. Notifications of events in the control are sent to the parent exactly as they are in the standard picker. Some extra public functions have been added to provide enhanced behavior. The public functions are listed below.

Functions

  • BOOL Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID);

    Offers the same behaviour as CDateTimeCtrl.

  • CEdit* GetEditControl() const;

    Returns a pointer to the edit control part of the control, to allow direct manipulation of its properties.

  • void EnableButton(BOOL bEnable = TRUE);

    Enables or disables the button used to drop down the Calendar control.

  • void RestoreFocus(BOOL bRestore = TRUE);

    Determines the behavior when clicking on the button. If TRUE, the control resets the focus back to the window which had the focus on clicking the button. Otherwise, the focus is set to the edit control. If the control is not editable, focus is always set back to the previously focused window. The default is for the focus to be set to the edit control.

  • void SetNonEditable(BOOL bNonEditable = TRUE);

    Used to make the edit portion of the control non-editable. The default is editable.

  • BOOL GetNonEditable();

    Returns whether the edit control is currently editable.

  • virtual BOOL IsValidDate(LPCTSTR lpszDate = NULL);

    Returns whether the date string passed is valid. If lpszDate is NULL then it returns whether the text in the edit control is a valid date string.

  • void SetValidCharsOnly(BOOL bValidCharsOnly = TRUE);

    Sets whether the user can only enter characters that are valid.

  • BOOL GetValidCharsOnly();

    Returns whether the user can only enter valid characters into the edit control.

  • void SetValidChars(LPCTSTR lpszValidChars = NULL);

    Sets the characters that are valid for the user to type into the edit control. If NULL is specified, the default characters are used (0-9 and the date separator of the current user's locale). If the user changes the locale settings, then the control will detect this and use the new separator.

  • CString GetValidChars();

    Returns the characters that have been set as being valid for the user to type into the edit control.

  • void SetAllowUpDownKeys(BOOL bAllow = TRUE);

    Sets whether the up/down arrow keys will increment/decrement the part of the date string that contains the cursor.

  • BOOL GetAllowUpDownKeys();

    Returns whether the up/down keys will increment/decrement parts of the date string.

  • BOOL SetButtonImageID(UINT nID, UINT nDisabledID = 0);

    Specifies a resource ID from which to load the button image. For the best results, the image should be 13 pixels wide and 12 pixels high. nDisabledID optionally specifies an image to use for the button in its disabled state.

  • UINT GetButtonImageID(UINT* pDisabledID = NULL) const;

    Returns the current button image's ID. The ID used for the disabled button can be retrieved in pDisabledID.

  • void GetButtonImageIDs(UINT& nID, UINT& nDisabledID) const;

    Retrieves the current button images' IDs.

  • void SetReadOnly(BOOL bReadOnly = TRUE);

    Sets the control to read-only. This is similar to the disabled state, but the text is not shown in grey and may be selected.

  • BOOL GetReadOnly() const;

    Returns whether the control is set to read-only.

History

  • 21st-Jan-2008 - Version 3.7
    • Updated to work correctly on Vista.
  • 20th-Apr-2007 - Version 3.6.1
    • Fixed missing and incorrect NM_KILLFOCUS notifications.
    • Changed to compile correctly in VC7 (Visual Studio 2003) and VC8 (Visual Studio 2005).
    • Added fix to control being shown if hidden in initializing of parent.
  • 22nd-Dec-2005 - Version 3.6
    • Added "read-only" state, to allow a semi-disabled state.
    • Updated to recognize XP theme changes and re-draw accordingly.
    • Fixed bug where the calendar could be shown in non-editable mode on F4, and the date could be changed by using up/down keys.
    • Added support for a "disabled" image to be used when the control is disabled or read-only.
    • Fixed bug where creating control with ES_READONLY style failed.
    • Nested button/edit/calendar/XP-style classes so they don't appear in the top level of the ClassView.
  • 04th-Apr-2005 - Version 3.5
    • Updated to support XP-styling.
    • Changed to grey-out edit if disabled (suggested by Hans-Georg Ulrich).
    • Added fix to enable compilation with Visual Studio .NET (thanks to Mark Harmon).
  • 09th-Jul-2004 - Version 3.4
    • Fixed bug where dismissing the month menu in the calendar control by clicking outside of the calendar area then prevents the calendar being dismissed without first clicking back on the calendar control (reported by winkle.)
    • Fixed assertion failures on re-creation of the control when two or more instances of the control are on a dialog (reported by pimOOsse and Rick Austin).
  • 09th-Jul-2003 - Version 3.3
    • Updated to support Unicode.
  • 15th-Apr-2003 - Version 3.2
    • Extended control to accept the DTM_SETRANGE and DTM_GETRANGE messages.
    • Fixed incorrect implementation of DTS_RIGHTALIGN.
    • Added facility to use a bitmap for the button instead of a down arrow.
    • WM_NOTIFY notifications now passed to the parent window from date control's children.
    • Fixed bug where clicking on the current (circled) day when the edit control is empty closed the calendar and did not put the selected day in the edit control.
  • 20th-Jan-2003 - Version 3.1
    • Fixed bug where, if the new date text is set in the DTN_DATETIMECHANGE handler (for instance, to constrain the date within some boundary) and the user selects a date which is "out of bounds" and the calendar closes, the selected date is copied to the edit control, overwriting the "fixed" date.
  • 17th-Jan-2003 - Version 3
    • Fixed bug where a call to Get/SetWindowText, SetFont or any function which resulted in sending of a DTM_XXX message caused an assertion the first time round, if called from the OnInitDialog of a parent dialog or equivalent.
  • 07th-Jan-2003 - Version 2
    • Edit control notifications (e.g. EN_CHANGE) now sent to the date control's parent window.
    • Added features as suggested by Marc Clifton:
      • Added function to validate a date string (IsValidDate).
      • Added function to only allow valid characters to be typed into the edit control. Also added a function to set the valid characters. The default valid characters are 0-9 and the date separator of the current user's locale. If the user changes the locale settings, then the control will automatically pick this up and use the new separator.
      • Added ability to use up/down keys to edit portions of the date text. The parent is sent DTN_DATETIMECHANGE notifications as the date changes.
  • 03rd-Jan-2003 - Version 1
    • First version.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Paul Vickery
Software Developer (Senior)
United Kingdom United Kingdom
Originally from an electronics background, I moved into software in 1996, partly as a result of being made redundant, and partly because I was very much enjoying the small amount of coding (in-at-the-deep-end-C) that I had been doing!
 
I swiftly moved from C to C++, and learned MFC, and then went on to real-time C on Unix. After this I moved to the company for which I currently work, which specialises in Configuration Management software, and currently program mainly in C/C++, for Windows. I have been gradually moving their legacy C code over to use C++ (with STL, MFC, ATL, and WTL). I have pulled in other technologies (Java, C#, VB, COM, SOAP) where appropriate, especially when integrating with third-party products.
 
In addition to that, I have overseen the technical side of the company website (ASP, VBScript, JavaScript, HTML, CSS), and have also worked closely with colleagues working on other products (Web-based, C#, ASP.NET, SQL, etc).
 
For developing, I mainly use Visual Studio 2010, along with an in-house-designed editor based on Andrei Stcherbatchenko's syntax parsing classes, and various (mostly freeware) tools. For website design, I use Dreaweaver CS3.
 
When not developing software, I enjoy listening to and playing music, playing electric and acoustic guitars and mandolin.
Follow on   Google+   LinkedIn

Comments and Discussions

 
Questionhow to change background color of the CDatetimeCtrl in MFC Pinmemberhasmukginoya21-Jan-13 2:01 
GeneralHeap Corruption on Vista Pinmemberthomas210827-Apr-10 3:06 
GeneralWindows Classic Theme on Server 2008 & Vista PinmemberRich Holliday8-Jan-09 2:03 
GeneralDemo does not set valid chars in OnInitDialog PinmemberDimitris Vassiliades18-Apr-08 0:12 
QuestionC# .NET version of this editable date time picker? Pinmemberkiloblaster18-Feb-08 20:02 
AnswerRe: C# .NET version of this editable date time picker? PinmemberPaul S. Vickery25-Feb-08 11:15 
GeneralDetected resource leak PinmemberPetitPapaNoël30-Aug-07 3:17 
GeneralRe: Detected resource leak PinmemberPaul S. Vickery25-Jan-08 0:54 
GeneralQuestion about the edit control Pinmemberkiloblaster28-Aug-07 15:45 
GeneralRe: Question about the edit control PinmemberPaul S. Vickery25-Jan-08 0:48 
GeneralProgram crashes!!! Please help! Pinmemberkiloblaster27-Aug-07 22:00 
GeneralNEW VERSION POSTED PinmemberPaul S. Vickery20-Apr-07 0:20 
GeneralShowWindow Issue PinmemberKevin Harker26-Mar-07 8:23 
AnswerRe: ShowWindow Issue PinmemberPaul S. Vickery30-Mar-07 2:30 
QuestionVS 2005 Compilable version? Pinmemberkiloblaster10-Mar-07 5:09 
AnswerRe: VS 2005 Compilable version? PinmemberPaul S. Vickery30-Mar-07 2:32 
GeneralRe: VS 2005 Compilable version? Pinmemberdressman198119-Apr-07 4:31 
GeneralAnurag jain PinmemberVodka_Chill29-Sep-06 20:57 
QuestionCatch KillFocus Message PinmemberPetitPapaNoël17-May-06 22:28 
AnswerRe: Catch KillFocus Message PinmemberPaul S. Vickery18-May-06 0:56 
GeneralRe: Catch KillFocus Message PinmemberPetitPapaNoël18-May-06 1:50 
NewsRe: Catch KillFocus Message PinmemberPetitPapaNoël18-May-06 2:36 
GeneralVS2003 Compilable version Pinmembernoyb66911-May-06 13:37 
AnswerRe: VS2003 Compilable version PinmemberPetitPapaNoël11-May-06 23:18 
AnswerRe: VS2003 Compilable version PinmemberPaul S. Vickery18-May-06 0:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411023.1 | Last Updated 24 Jan 2008
Article Copyright 2003 by Paul Vickery
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid