Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling dialog OK button MFC

Tags:

c++

dialog

mfc

How do I disable MFC dialog OK button?
This code:
CWnd* fieldOK = pDlg->GetDlgItem(IDOK);
fieldOK->EnableWindow(FALSE);

causes exception "Access violation reading location..." in line ASSERT(::IsWindow(m_hWnd) || (m_pCtrlSite != NULL)); of function CWnd::EnableWindow(BOOL bEnable) in winnocc.cpp from mfc90d.dll
In this time focus is on another control.
What's can be wrong?

Thanks for help.

[EDITED]

bool CSCalcNormCell::OnSelectionChanged( CWnd* pDlg, int type, int page, UINT ctrl_id ) 
{ 
  DDX_DataBox(pDX.get(), IDC_WORKSHOP_COMBO, ws_code); 
  if (!CInfactoryPriceAdapter::CanEditPricesForWorkshop( ws_code )) 
  { 
    CWnd* fieldOK = pDlg->GetDlgItem(IDOK); 
    fieldOK->EnableWindow(FALSE); 
  } 
  else 
  { 
    CWnd* fieldOK = pDlg->GetDlgItem(IDOK); 
    fieldOK->EnableWindow(TRUE); 
  } 
}
like image 327
GrinderZ Avatar asked Jun 21 '13 11:06

GrinderZ


2 Answers

I'm not sure why would wouldn't be able to do it. If I take a regular CDialog and I do an init like this:

BOOL CMyDialog::OnInitDialog() {
    CDialog::OnInitDialog();
    CWnd *okbtn = GetDlgItem( IDOK );
    if ( okbtn ) {
        okbtn->EnableWindow( FALSE );
    }
    return TRUE;
}

it disables the button just fine. Perhaps something else is wrong?

like image 140
mark Avatar answered Nov 11 '22 00:11

mark


Try this: http://support.microsoft.com/kb/122489

How to Disable Default Pushbutton Handling for MFC Dialog

Although default button (pushbutton) support is recommended, you might want to disable or modify the standard implementation in certain situations. You can do this in an MFC application by following these steps:

Load the dialog into App Studio and change the OK button identifier from IDOK to something else such as IDC_MYOK. Also, clear the check from Default Button property.

Use ClassWizard to create a message handling function for this button named OnClickedMyOK. This function will be executed when a BN_CLICKED message is received from this button.

In the code for OnClickedMyOK, call the base class version of the OnOK function. Here is an example:

void CMyDialog::OnClickedMyOK()
   {
      CDialog::OnOK();
   }

Override OnOK for your dialog, and do nothing inside the function. Here is an example:

void CMyDialog::OnOK()
   {
   }

Run the program and bring up the dialog. Give focus to a control other than the OK button. Press the RETURN key. Notice that CDialog::OnOK() is never executed.

like image 28
Leo Chapiro Avatar answered Nov 10 '22 23:11

Leo Chapiro