Year, Month and Date Selector Control

(22935 bytes)

The original program and article was written by Shekar Narayanan. It allowed for the month and the date to be selected. In the updated version, the date, month and year can be selected (from the year 1583 onwards; yes it is Year 2000 compliant!!).

What is this?

This is a custom control which allows you to select a date, a month and year from a drop down window.

This custom control actually consists of two classes - one derived from CButton which acts like the regular combo box and other derived directly from generic CWnd object which is the pop-up window.

The CButton derived object is the one you would use in the dialog boxes or form views and it is called CYMSelector.

When CYMSelector receives the message 'clicked', it will create the pop-up window and disables the parent window from any activity. The user can either make the selection using the mouse or press Esc key to abort. The - and + sign at the top corners of the window allows the selection of the year.

The default color of the window is controlled by the OS's settings. Since you have the source, you could control it if you want to.

How do I use it in my project?


  1. Insert the YMSelector.cpp in your project and include the YMSelector.h in stdafx.h.
  2. Open the Dialog box or Form View resource and insert a regular button control
  3. Change the property of this button to Owner Draw
  4. From Class Wizard, create a member variable for this button - as a control.
  5. Open the corresponding header file and change the variable type from CButton to CYMSelector
  6. Alternatively, you could rebuild the .clw file by deleting it. This way, from class wizard, you can directly create the variable as CYMSelector instead of CButton, and you don't have to change the header file.

That is it. It has the following public methods to play around:

  • GetYear()
    Returns the selected year as integer
  • GetMonth()
    Returns the selected month as integer 1 for Jan and 12 for Dec
  • GetDate()
    Returns the selected month as integer from 1 to 31
  • GetMonthString()
    Returns the selected month as string like 'January'
  • SetYear(int)
    Sets the year value for the control. Pass a value like 1998 etc...
  • SetMonth(int)
    Sets the month value for the control. Pass between 1 and 12
  • SetDate(int)
    Sets the date value for the control. Pass between 1 and 31
Download demo project (and sourece) - 25 KB



Comments

  • ExCalendar

    Posted by Legacy on 01/30/2002 12:00am

    Originally posted by: Mike Philips

    If you would like to check, please feel free to download a nice calendar control here
    
    http://www.exontrol.com/sg.jsp?content=products/excalendar

    Regards, Mike

    Reply
  • Two enhancements plus some minor changes (modified)

    Posted by Legacy on 01/20/1999 12:00am

    Originally posted by: M Pederson

    I made a few additions to the code.  First, the code supports left and
    
    right arrows to change the date and escape to abandon the popup but no
    key is provided to accept the currently displayed date. I added the
    following code to function CYMPopUp::OnKeyDown() to use the return key
    to accept the currently displayed date:

    else if (nChar == VK_RETURN) {
    m_pParent->EnableWindow(true);
    int nComposite = m_nDate + 100*(m_nMonth+1);
    m_pParent->PostMessage(
    YM_SELECTED, (WPARAM)m_nYear, (LPARAM)nComposite);
    DestroyWindow(); // VERY IMPORTANT! See below!
    }

    Second, I think in most Calendar controls, the popup will be aborted if
    it loses focus. This can be implemented by handling WM_KILLFOCUS in
    class CYMPopUp.

    void CYMPopUp::OnKillFocus(CWnd* pNewWnd)
    {
    CWnd::OnKillFocus(pNewWnd);

    m_pParent->EnableWindow(true);
    m_pParent->SendMessage(YM_ABORTED);
    DestroyWindow();
    }

    You can create the function and message handlers through ClassWizard,
    then substitute the above code. This is precisely the same as hitting
    the escape key. Also, this may solve the problem brought up by S Tanna
    in a prior comment.

    VERY IMPORTANT!: If you implement the CYMPopUp::OnKillFocus() function
    as I wrote it, you MUST remove the call to DestroyWindow() from
    handling of both VK_ESCAPE and VK_RETURN. Otherwise, when CYMSelector
    sets focus back to its parent, DestroyWindow() will be called twice,
    once through OnKillFocus() and once through CYMPopUp::OnKeyDown().

    Finally, all code in YMSelector.h, except for the declaration of class
    CYMPopUp, can be moved to file YMSelector.cpp unless they want to use
    the calendar popup in a window. This is a rather picky point but
    outside objects usually will have no use for this code so it shouldn't
    ordinarily be exposed unless needed.

    I'd like to thank Snubby Rajan for submitting this code. I enjoyed
    looking through it to see how it was implemented.

    Michael


    Reply
  • Another bug - with fix

    Posted by Legacy on 11/13/1998 12:00am

    Originally posted by: S Tanna

    I found another bug

    If used on a property page, you can still switch between property
    pages on the property sheet when the list is dropped. This is
    clearly not what the author intended.

    Doing a global replace of GetParent() with GetParentFrame() in
    YMSelector.cpp seems to fix it, but I haven't had time to
    full test this out yet.

    Regards

    webmaster@ans2000.com

    http://www.ans2000.com (Year 2000 Information)
    http://www.wildcomputer.com (General Computer information)


    Reply
  • Bug with YMDPicker on months with Sunday as first day of the month

    Posted by Legacy on 11/10/1998 12:00am

    Originally posted by: Roland Lopes

    There's a bug in this class that will never allow Sunday to be the first day of a month, monday becomes the
    first.	To see this, try displaying Feb, Mar or Nov 1998.  A quick fix is:
    
    

    Locate this line in the file "YMSelector.cpp", procedure "OnPaint":
    -----------------

    if (!bDone && ((j+1) == nFirstDayofMonth || nFirstDayofMonth == 0))

    ------------------
    Replace it with:
    ------------------

    if(nFirstDayofMonth == 0)
    bDone = TRUE;

    if (!bDone && ((j+1) == nFirstDayofMonth))

    -------------------

    That's it!


    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • You probably have several goals for your patient portal of choice. Is "community" one of them? With a bevy of vendors offering portal solutions, it can be challenging for a hospital to know where to start. Fortunately, YourCareCommunity helps ease the decision-making process. Read this white paper to learn more. "3 Ways Clinicians can Leverage a Patient Portal to Craft a Healthcare Community" is a published document owned by www.medhost.com

  • The rapid evolution of enterprise storage technologies, combined with external forces, like the explosion of big data, can cause Linux® and server administrators to play catch-up when it comes to storage. Running a bunch of monolithic storage devices and proprietary, disconnected technologies forces administrators to spend valuable time creating and managing complex solutions. To reduce complexity and enable rapid deployment of new technologies and applications, server administrators need a single open …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds