Disable OK dialog MFC button

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

raises an exception "A place for detection of access violation ..." in the line ASSERT(::IsWindow(m_hWnd) || (m_pCtrlSite != NULL)); CWnd::EnableWindow(BOOL bEnable) winnocc.cpp in winnocc.cpp from mfc90d.dll
At this time, the focus is on another control.
What could be wrong?

Thanks for the help.

[Changed]

 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); } } 
+6
source share
5 answers

I'm not sure why not do it. If I take a regular CDialog and I do 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. Maybe something else is wrong?

+4
source

Try the following: http://support.microsoft.com/kb/122489

How to disable default button processing for MFC dialog

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

Download the dialog in App Studio and change the OK button identifier from IDOK to something else, such as IDC_MYOK. Also clear the check from the Button property by default.

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

In the OnClickedMyOK code, 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 call up the dialog. Give focus to control other than the OK button. Press the RETURN key. Note that CDialog :: OnOK () is never executed.

+2
source

I suspect the problem is with the pDlg pointer. When you call pDlg->GetDlgItem(IDOK) , an already existing dialog box is already created?

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

That is why I think mark answer is very close. You can disable it on mark answer is very close. You can disable it on OnInitDialog` or other members of your dialog class after it appears.

+1
source

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

In any case, you must protect your code from crashes. It is better that your code does nothing to minimize the application. Restructuring thus eliminates the issue of access violation due to a 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); } } 
0
source

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

0
source

Source: https://habr.com/ru/post/947828/


All Articles