How to set a blank date as the initial value for Date Time Picker control (238077)



The information in this article applies to:

  • Microsoft Visual C++, 32-bit Enterprise Edition 5.0
  • Microsoft Visual C++, 32-bit Enterprise Edition 6.0
  • Microsoft Visual C++, 32-bit Professional Edition 5.0
  • Microsoft Visual C++, 32-bit Professional Edition 6.0
  • Microsoft Visual C++, 32-bit Learning Edition 6.0
  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET (2003)
  • Microsoft Visual C++ .NET (2002)

This article was previously published under Q238077
Note Microsoft Visual C++ .NET 2002 and Microsoft Visual C++ .NET 2003 support both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code. Microsoft Visual C++ 2005 supports both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model.

SUMMARY

Both Windows Date Time Picker control (CDateTimeCtrl) and Microsoft Date Time Picker ActiveX controls have the current date as the initial value by default. This article explains how to set a blank date as initial value for these controls.

MORE INFORMATION

Windows Date Time Picker control and Microsoft Date Time Picker ActiveX controls change the time back to the current date even if you set it to blank by calling SetWindowText with a blank string as parameter.

The following steps describe a way to set a blank date as the initial value for Microsoft Date Time Picker ActiveX control.
  1. Change the format type to custom format (3) and set the custom format to where one blank string.m_datePicker is a member variable created for the Windows Date Time Picker control placed on a dialog box.
       BOOL CDtpickerDlg::OnInitDialog()
       {
           CDialog::OnInitDialog();
    
           SetIcon(m_hIcon, TRUE);                 // Set big icon
           SetIcon(m_hIcon, FALSE);                // Set small icon
           
           m_datePicker.SetFormat(3);         // Change the format type to    custom format.
           m_datePicker.SetCustomFormat(" "); // One blank string.
    
           return TRUE;  // return TRUE  unless you set the focus to a control
       }
    
    						
  2. Add an event handler for the Change event in your dialog class using ClassWizard.
       ON_EVENT(CDtpickerDlg, IDC_DTPICKER1, 2,OnChangeDtpicker1,VTS_NONE)
    						
    NOTE: If using Visual Studio.NET do the following: In the Property browser, on the Events tav, add an event handler for the Change event in your dialog class.
  3. In the event handler (OnChangeDtPicker1) change the format back to the format you want.
       void CDtpickerDlg::OnChangeDtpicker1() 
       {
           m_datePicker.SetFormat(1);
       }
    						
You could use similar logic to set a blank date as an initial value for Windows Standard Date Time Picker control.
  1. Call SetFormat with one blank string as parameter.
    m_dateCtrl.SetFormat(" "); //one blank
  2. Add a notification message handler for the DTN_DATETIMECHANGE message in your dialog class and add the following code to it. Change the format type to the format you want.
       void CDateTimeCtrlDlg::OnDateTimeChange(NMHDR *pNotifyStruct, LRESULT    *result)
       {
            m_dateCtrl.SetFormat(DTS_SHORTDATEFORMAT);
            *pResult = 0;
       }
    						
(c) Microsoft Corporation 1999, All Rights Reserved. Contributions by Sreedhar Pelluru, Microsoft Corporation.


Modification Type:MajorLast Reviewed:1/10/2006
Keywords:kbControl kbCmnCtrls kbCtrl kbDateTime kbhowto KB238077 kbAudDeveloper