Use the Calendar Control 8.0 as a Date Picker Control

Download demo project.

The Calendar Control 8.0 (mscal.ocx) shipping with Visual Studio 5.0 looks very nice. It can be very simple incorporate in your application as a date picker control.

It has two combo box to quickly change a month and a year. Unfortunetly, when you try to change the month or the year the date sets on NULL value and doesn't highlighted.

In my example I implement a full date picker control using Calendar Control 8.0. I spend some time to find solution how to work with year's and month's comboboxes and I think it will be usefull to share my experience.

I found that IDs of the Month and the Year comboboxes have the respective values 1 and 2. Then, I trap the message event "NewYear"( "NewYear") and when the combobox obtains focus change the year(month) using NextYear()(NextMonth()) and PreviousYear()(PreviousMonth()) methods.

This is a code for NewYear event:


void CDateCal::OnNewYearDateCalendar() 
{
	// TODO: Add your control notification handler code here
	int nID;

	CWnd* pWnd = GetFocus();
	nID = pWnd->GetDlgCtrlID();	
	CString string;
	pWnd->GetWindowText( string );
	if(2==nID) {//nID =2 - ID combobox Year
		int nDif = atoi(string) - m_nYear;
		if(nDif > 0){	//if increase Year
			while(nDif--)
				OnNextYear();//change year
		}
		else if(nDif < 0){	//if decrease Year
			while(nDif++)
				OnPrevYear();//change Year in a calendar
		}
	}
	
}



Comments

  • ExCalendar

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

    Originally posted by: Mike Philips

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

    Regards,
    Mike

    Reply
  • NEED HELP!!!!!!

    Posted by Legacy on 04/18/2001 12:00am

    Originally posted by: Nick

    Can somebody send me the visual basic code for this testcalendar project..PLEASE!!!!!!!!!!!

    Reply
  • How to retreive the week days of the date?

    Posted by Legacy on 01/13/2000 12:00am

    Originally posted by: Sammy

    I want to get the week day of the date, say, I want to get the "Thursday" when the picker is pointed to 13/1/2000, how to do this? Thank you!

    Reply
  • Date picker works on some machines, but doesn't work on others?

    Posted by Legacy on 08/17/1999 12:00am

    Originally posted by: Erich Ruth

    I really like the date picker and I incorporated it into a program I wrote. I sent the program to several different people and for some, the date picker works, but for others it doesn't work even though it was the exact same code. Are there any hardward requirements for using this code or anything I can do to determine why one computer can successfully use this code and why another computer cannot?

    Please, any response you can give me will be greatly appreciated.

    Reply
  • Use the Calendar Control 8.0 as a Date Picker Control

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

    Originally posted by: Viseth Peang

    Hello,
    
    

    I was just wondering if you were able to use mscal.ocx on alpha platform, or do you know whether or not it is supported for alphas? Any comments is greatly appreciated.

    Thanks

    Viseth

    Reply
  • MSVC++ 6.0 & COMCTL32.DLL 4.72

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

    Originally posted by: Martin Rub�

    Yes. VS 6.0 is shipped with MSIE 4.01 common control
    library (4.72) and MFC library contains classes for
    these common controls including date picker control.

    Dialog editor and class wizard also support these
    classes (this feature is missing in MSVC 5.0).

    It has one big negative :
    - I don't like 'red-circle' around the selected day.
    Highlited rectangle would be better by my opinion.

    Merlin.

    Reply
  • Some good remarks

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

    Originally posted by: Alexey Vl

    I would like to express my thanks for the author of  this mail.
    
    He found how to improve my code and fix some drawback in year
    (month) comboboxes when year(month ) changes.
    __________________________________________________________________________
    From: cc62@dial.pipex.com (P CONGRAVE)
    Re "Use the Calendar Control 8.0 as a Date Picker Control" from MFC Programmers SourceBook.

    I found that when i changes the year using the combo, the combo scrolled slowly to the required year.
    So i asume that you have had the same problem!
    Here is a quick fix. SetDraw to false to stop the combo from updating itself. Setfocus so that the combo closes. The Month combo can be have simular changes made.

    void CDateCal::OnNewYearDateCalendar()
    {
    int nID;
    CWnd* pWnd = GetFocus();
    nID = pWnd->GetDlgCtrlID();
    if(2==nID)
    {
    CString string;
    pWnd->GetWindowText( string ); // only get text here to avoid accessing controls that we are not //interested in
    pWnd->SetRedraw(FALSE); // Diable Combo Updating
    SetFocus(); // Setfocus to the dialog to avoid reentery
    int nDif = atoi(string) - m_nYear;
    if(nDif>0)
    {
    while(nDif--)
    {
    m_calDate.NextYear();
    }
    } else
    {
    if(nDif<0)
    {
    while(nDif++)
    {
    m_calDate.PreviousYear();
    }
    }
    }
    pWnd->SetRedraw(TRUE);
    }
    }

    Reply
  • MSJ and VC6

    Posted by Legacy on 09/29/1998 12:00am

    Originally posted by: phil swann

    I have just read the article in MSJ regarding VC6 support for latest common controls and it appears that the latest version of MFC wraps the date controls functionality

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

Top White Papers and Webcasts

  • The explosion in mobile devices and applications has generated a great deal of interest in APIs. Today's businesses are under increased pressure to make it easy to build apps, supply tools to help developers work more quickly, and deploy operational analytics so they can track users, developers, application performance, and more. Apigee Edge provides comprehensive API delivery tools and both operational and business-level analytics in an integrated platform. It is available as on-premise software or through …

  • Best-in-Class organizations execute on a strategy that supports the multi-channel nature of customer requests. These leading organizations do not just open up their service infrastructures to accommodate new channels, but also empower their teams to deliver an effective and consistent experience regardless of the channel selected by the customer. This document will highlight the key business capabilities that support a Best-in-Class customer engagement strategy.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds