Wesley
Wesley

Reputation: 307

How do I prevent my MFC dialog's window from closing using CPropertyPage::OnOk()?

I'm working on a C++ project and have a CPropertyPage::OnOk() method.

What I'd like to happen is when the user clicks Ok or Apply, the program will perform a check, if the check is wrong, it'll supress the window from closing.

How would I go about to supressing the window from closing?

I've tried a simple return but no go.

For example:

void CApptEditGen::OnOK() 
{
    if ( prealloc(&m_ai->apapallocate) || Dummy_aftalloc(m_ai) == REDO ) {
        m_pCtl_ApptEdit_Units->SetFocus();
        m_pCtl_ApptEdit_Units->SetWindowText("");
        return;
    }
    CPropertyPage::OnOK();  
}

Upvotes: 3

Views: 5470

Answers (2)

Wesley
Wesley

Reputation: 307

Used the following to check if value A > value B then return 0 to stop from closing!

BOOL CApptEditGen::OnKillActive()
{
    CString inpValue;
    m_pCtl_ApptEdit_Units->GetWindowText(inpValue);
    if (atoi(inpValue) > freeUnitsAvailable)
        return 0;

    return CPropertyPage::OnKillActive();
}

Upvotes: 2

razlebe
razlebe

Reputation: 7144

A simple return should do the trick, as demonstrated by the code snippet below from this page on MSDN, which describes the OnOK() function of CDialog (from which CPropertyPage is derived):

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

void CMyDialog::OnOK() 
{
   // TODO: Add extra validation here

   // Ensure that your UI got the necessary input 
   // from the user before closing the dialog. The 
   // default OnOK will close this.
   if ( m_nMyValue == 0 ) // Is a particular field still empty?
   {
      AfxMessageBox("Please enter a value for MyValue");
      return; // Inform the user that he can't close the dialog without
              // entering the necessary values and don't close the 
              // dialog.
   }

   CDialog::OnOK(); // This will close the dialog and DoModal will return.
}

Are you absolutely sure you've correctly overriden OnOK() on CPropertyPage? If not then the default CPropertyPage::OnOK will be called, which will close the window as you describe.

Upvotes: 1

Related Questions