Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable an Button in MFC Dialog

Tags:

mfc

I have two buttons:

  1. Radio button: "Hex"
  2. Button: "A"

I want to enable "A" anytime users "Hex" button is checked (the state of "A" is "Disabled" when it is created), how can I do that? Thank everyone.

The Calculator MFC Application

like image 481
Phùng Khánh Hiên Avatar asked Oct 05 '14 17:10

Phùng Khánh Hiên


2 Answers

You need to use CButton's EnableWindow function.

buttonA.EnableWindow( TRUE );

If you do not have the CButton object, you can access the button by calling GetDlgItem with its ID:

GetDlgItem( IDC_BUTTON_A )->EnableWindow( TRUE );
like image 136
Goz Avatar answered Sep 28 '22 00:09

Goz


You should use ON_UPDATE_COMMAND_UI mechanism to enable/disable the 'A' or any other button in your dialog. By default it is not available for dialog based application but you can easily enable them by following this article.

The code in your update function will look something like this:

void CCalculatorDlg::OnUpdateButtonA(CCmdUI* pCmdUI)
{
        if( m_ctrlBtnHex.GetCheck() == BST_CHECKED )
        {
            pCmdUI->Enable( TRUE );
        }
        else
        {
            pCmdUI->Enable( FALSE );
        }
}

In your case since A, B, C, D, E, F will essentially have same states so you can instead do this:

void CCalculatorDlg::OnUpdateButtonA(CCmdUI* pCmdUI)
{
        if( m_ctrlBtnHex.GetCheck() == BST_CHECKED) )
        {
            m_ctrlBtnA.EnableWindow( TRUE );
            m_ctrlBtnB.EnableWindow( TRUE );
            m_ctrlBtnC.EnableWindow( TRUE );
            // so on...
        }
        else
        {
            m_ctrlBtnA.EnableWindow( FALSE );
            m_ctrlBtnB.EnableWindow( FALSE );
            m_ctrlBtnC.EnableWindow( FALSE );
            // so on...
        }
}
like image 41
zar Avatar answered Sep 28 '22 00:09

zar