Disabling dialog OK button MFC
Asked Answered
S

6

7

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); 
  } 
}
Suboxide answered 21/6, 2013 at 11:1 Comment(0)
I
8

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?

Infiltration answered 21/6, 2013 at 12:14 Comment(0)
M
2

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.

Martyry answered 21/6, 2013 at 11:8 Comment(4)
Is it correct that this prevents action upon pressing OK but doesn't disable it?Tirewoman
If you believe the title of the article "How to Disable ...", no ;)Martyry
But I need disable/enable OK button dinamically. That's code: 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); } }Suboxide
In this case I would delete the standard OK button created by default and create my own Button (ID_MYBUTTON). This button you can disable like you already tried ...Martyry
L
1

I suspect the problem comes from pDlg pointer. When you call pDlg->GetDlgItem(IDOK), is the dialog already created already?

Make a breakpoint at the line CWnd* fieldOK = pDlg->GetDlgItem(IDOK); and debug into it to see if fieldOK pointer is null or a valid pointer.

That is why I think mark's answer is very close. You can disable it onOnInitDialog` or other members of you dialog class after it showed up.

Lucier answered 31/3, 2015 at 16:43 Comment(0)
T
0

The problem you have is that the button control has not been created on the interface yet. We do not get the full vision of your problem.

Anyway, you should protect your code from crashing. It is better that your code does nothing than to crash the application. Restructuring it like this avoids the access violation problem due to the NULL pointer:

bool CSCalcNormCell::OnSelectionChanged( CWnd* pDlg, int type, int page, UINT ctrl_id ) 
{ 
    DDX_DataBox(pDX.get(), IDC_WORKSHOP_COMBO, ws_code);

    CWnd* fieldOK = pDlg->GetDlgItem(IDOK); 
    if (fieldOK)
    {
        if (!CInfactoryPriceAdapter::CanEditPricesForWorkshop( ws_code )) 
            fieldOK->EnableWindow(FALSE); 
        else
            fieldOK->EnableWindow(TRUE); 
     }
}
Totalitarianism answered 23/7, 2015 at 23:32 Comment(0)
D
0

You need to load a bitmap for the disable mode of the OK button in LoadBitmaps() function.

Devondevona answered 8/3, 2016 at 15:14 Comment(1)
Hi, welcome to SO. This should actually be a comment, not an answer. You can comment once you earn enough reputation. Thanks.Ngocnguyen
M
0

I don't know what causes the exception, I was also getting it but I found an alternative way of disabling a button using command update handlers, following darwen's answer.

This works fine for me:

afx_msg void OnUpdateButton(CCmdUI *pCmdUI); 
void CYourClass::OnUpdateButton(CCmdUI *pCmdUI) {
    pCmdUI->Enable(enableButton);
}
ON_UPDATE_COMMAND_UI(ID_BUTTON, OnUpdateButton) // On message map
Mineralogy answered 18/4, 2023 at 12:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.