Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.


The framework calls this member function when the user clicks the Cancel button or presses the ESC key in a modal or modeless dialog box.

virtual void OnCancel( );

Override this member function to perform Cancel button action. The default simply terminates a modal dialog box by calling EndDialog and causing DoModal to return IDCANCEL.

If you implement the Cancel button in a modeless dialog box, you must override the OnCancel member function and call DestroyWindow from within it. Don't call the base-class member function, because it calls EndDialog, which will make the dialog box invisible but not destroy it.

/* MyDialog.cpp */
#include "MyDialog.h"

void CMyDialog::OnCancel() 
   // TODO: Add extra cleanup here
   // Ensure that you reset all the values back to the
   // ones before modification. This handler is called
   // when the user doesn't want to save the changes.
   if ( AfxMessageBox("Are you sure you want to abort the changes?", 
         MB_YESNO) == IDNO )
      return; // Give the user a chance if he has unknowingly hit the
              // Cancel button. If he says No, return. Don't reset. If
              // Yes, go ahead and reset the values and close the dialog.

   m_nMyValue = m_nPrevValue;
   m_pMyString = NULL; 


Community Additions

© 2015 Microsoft