MFC - Date & Time Picker



The date and time picker control (CDateTimeCtrl) implements an intuitive and recognizable method of entering or selecting a specific date. The main interface of the control is similar in functionality to a combo box. However, if the user expands the control, a month calendar control appears (by default), allowing the user to specify a particular date. When a date is chosen, the month calendar control automatically disappears.

Let us look into a simple example by creating a new MFC application.

Step 1 − Remove the Caption and set its ID to IDC_STATIC_TXT.

Step 2 − Add the value variable for text control.

DateTimePicker Text Control

Step 3 − Drag the Date Time Picker control.

Drag Date and Time Picker

Step 4 − Add a control variable for Date Time Picker.

DateTimePicker Var Control

Step 5 − Add the Event handler for Date Time Picker.

DateTimePicker Event

Step 6 − Here is the implementation of event handler.

void CMFCDateAndTimePickerDlg::OnDtnDatetimechangeDatetimepicker1(NMHDR *pNMHDR, LRESULT *pResult){ 
   LPNMDATETIMECHANGE pDTChange = reinterpret_cast  <LPNMDATETIMECHANGE>(pNMHDR); 
   // TODO: Add your control notification handler code here 
  
   GetDlgItemText(IDC_DATETIMEPICKER1, m_strValue); 
   UpdateData(FALSE); 
   *pResult = 0; 
} 

Step 7 − When you run the above application, you see the following output. Select any date, it will display on the Static Text Control.

Date and Time Picker
mfc_windows_controls.htm
Advertisements