Click to See Complete Forum and Search --> : Disabling Buttons


July 15th, 1999, 05:40 PM
how can I disable and enable command buttons in a simple dialog at runtime? I canīt find some kind of an instance name for a command button (the idname IDC_MYBUTTON surely doesnīt do it) thatīs of any use. Besides, I canīt even find a suitable CButton::Disable/Enable command.
What can i do?

Rail Jon Rogut
July 15th, 1999, 06:54 PM
Well in ClassWizard create a member variable of type Control for your button... This will create a member variable of type CButton in the header of your CDialog derived class. CButton is derived from CWnd, so through inheritance it inherits all of CWnd's functions, including EnableWindow().

So say you create a member variable in ClassWizard of type Control and name it m_CtrlButton, you can enable and disable it using the following code:

m_CtrlButton.EnableWindow(TRUE); // Enable

m_CtrlButton.EnableWindow(FALSE); // Disable


Rail

Recording Engineer/Software Developer
Rail Jon Rogut Software
railro@earthlink.net
http://home.earthlink.net/~railro/

Burlacu Ovidiu
July 16th, 1999, 04:57 AM
You can enable and disable it using the following code:


CWnd *pWndCtrl = GetDlgItem( ID_of your button)
pWndCtrl->EnableWindow( TRUE ) //Enable

pWndCtrl->EnableWindow( FALSE ) //Enable



In this way you can Enable/Disable any control from a dialog

John Killingbeck
July 16th, 1999, 10:14 PM
From the ProgramView.cpp file - use class wizard to select the IDC_xxx for the button in the CProgramView Object ID's window and UPDATE_COMMAND_UI in the Messages window. Create the function framework as ClassWizard requests. The function name will be CProgramView::OnUpdateControlName. Then see the code below for an example for a button with the ID of ID_RETRIEVE_1.

void CMandelbrotView::OnUpdateRetrieve1(CCmdUI* pCmdUI)
{
// Programmer supplied code
if ( // Enable condition here // )
pCmdUI->Enable( TRUE );
else
pCmdUI->Enable( FALSE );
// End programmer supplied code
}




The class wizard supplies the pointer to the ID in the call. The pCmdUI pointer automatically points to the proper control ID. The Enable call either enables or disables the control. This uses ClassWizard features to reduce the chance for programmer error.