Enable or Disable Controls

Enabling or Disabling controls in more effective and efficient way

I was doing a project in which there were many controls on a dialog box. This dialog box could display for different user interactions. At different interactions some controls on the dialog box become enable and some become disable. To do the enabling and disabling, I make a decision based on some variable which determines the controls that are going to be enable or disable. e.g.

if( m_bButton )
{
  GetDlgItem( IDC_BUTTON1 )->EnableWindow(TRUE);
  GetDlgItem( IDC_BUTTON2 )->EnableWindow(TRUE);

  GetDlgItem( IDC_EDIT2 )->EnableWindow(FALSE);
  GetDlgItem( IDC_EDIT3 )->EnableWindow(FALSE);

  GetDlgItem( IDC_COMBO1 )->EnableWindow(FALSE);
  GetDlgItem( IDC_COMBO2 )->EnableWindow(FALSE);
}
else if( m_bEdit )
{
  GetDlgItem( IDC_BUTTON1 )->EnableWindow(FALSE);
  GetDlgItem( IDC_BUTTON2 )->EnableWindow(FALSE);

  GetDlgItem( IDC_EDIT2 )->EnableWindow(TRUE);
  GetDlgItem( IDC_EDIT3 )->EnableWindow(TRUE);

  GetDlgItem( IDC_COMBO1 )->EnableWindow(FALSE);
  GetDlgItem( IDC_COMBO2 )->EnableWindow(FALSE);
}
else if( m_bCombo )	
{
  GetDlgItem( IDC_BUTTON1 )->EnableWindow(FALSE);
  GetDlgItem( IDC_BUTTON2 )->EnableWindow(FALSE);

  GetDlgItem( IDC_EDIT2 )->EnableWindow(FALSE);
  GetDlgItem( IDC_EDIT3 )->EnableWindow(FALSE);

  GetDlgItem( IDC_COMBO1 )->EnableWindow(TRUE);
  GetDlgItem( IDC_COMBO2 )->EnableWindow(TRUE);
}
The above solution is fine; however, there are twoO obvious issues:

  • Code Replication : There is lot of replication of code that enable or disable various controls.
  • Code Maintenance: If this is added into future a controls, then its state has to be updated in all the functions that are enabling or disabling the controls. Chance is that we might forget to update it at some places.
  • A more effective solution to this problem can be achieved by using the "bitset class" of STL. Using the "bitset class" a sequence consisting of a number of bits cna be saved. This provides a compact and effective way of keeping flags for a set of items (controls in our problem).

    In the demo project, there are two command buttons, two edit controls and two combo boxes that are going to be enable or disable when the user click the corresponding radio buttons. Initially all the controls are disabled. When a user click the radio button labeled "Enable Buttons" then the Buttons are going to be enabled and the other controls will remain disabled. Other radio buttons will work in a similar manner. To solve this problem we are going to declare an object of bitset class in dialog class as follows:

    bitset<16> m_bitControls;

    Remember to include following two lines to use bitset class:

    #include <bitset>
    using namespace std;
    

    Now define macos for the six contols on the dialog box as follows:

    #define	BUTTON_ONE      0
    #define	BUTTON_TWO      1
    #define	EDIT_CTRL_ONE   2
    #define	EDIT_CTRL_TWO   3
    #define	COMBO_ONE       4
    #define	COMBO_TWO       5
    

    Above mentioned macros will use as an Index value in the bitset object.

    Now initialize the bitset object in initialization list as follows:

    m_bitControls(0)

    Above line will set all the 16 bit to O.

    Add a member function which will enable or disable the controls depend on the value of bits of the bitset object. Code of this functions is as follow:

    void CDialogAppDlg::enableORdisableCtrls()
    {
      GetDlgItem( IDC_BUTTON1 )->EnableWindow(m_bitControls[BUTTON_ONE]);
      GetDlgItem( IDC_BUTTON2 )->EnableWindow(m_bitControls[BUTTON_TWO]);
    
      GetDlgItem( IDC_EDIT2 )->EnableWindow(m_bitControls[EDIT_CTRL_ONE]);
      GetDlgItem( IDC_EDIT3 )->EnableWindow(m_bitControls[EDIT_CTRL_TWO]);
    
      GetDlgItem( IDC_COMBO1 )->EnableWindow(m_bitControls[COMBO_ONE]);
      GetDlgItem( IDC_COMBO2 )->EnableWindow(m_bitControls[COMBO_TWO]);
    }
    

    Now in the one of the handler function for clicking Radio button add the following code.

    m_bitControls[BUTTON_ONE] = TRUE;
    m_bitControls[BUTTON_TWO] = TRUE;
    
    m_bitControls[EDIT_CTRL_ONE] = FALSE;
    m_bitControls[EDIT_CTRL_TWO] = FALSE;
    
    m_bitControls[COMBO_ONE] = FALSE;
    m_bitControls[COMBO_TWO] = FALSE;  
    
    enableORdisableCtrls();
    

    Above code will set the first and second bit and reset the last four bits. After that enableORdisble function is executed. This code will enable the two button controls and disable two edit controls and two combo boxes. For the other two handlers see the code included with this article.

    This technique provide solutions for two above mentioned issues:

  • Code Replication : Now there is only one function which have the code to enable to disable the controls.
  • Code Maintenance: Since there is only one function then maintenance of code is not a big task.
  • Downloads

    Download source - 37 Kb


    Comments

    • win32 api:

      Posted by dskv on 08/26/2010 10:18am

      The top example did not work for me but this did: EnableWindow( GetDlgItem(hDlg, IDC_YOURIDC), false); //disable EnableWindow( GetDlgItem(hDlg, IDC_YOURIDC), true ); //enable

      Reply
    • so easy jamil...the simplest way to enable and disable

      Posted by Legacy on 08/15/2003 12:00am

      Originally posted by: saeed AL-hamed

      good work jamil

      Reply
    • thanks

      Posted by Legacy on 12/03/2002 12:00am

      Originally posted by: shaumei

      thanks!

      Reply
    • Thanks a lot

      Posted by Legacy on 10/01/2002 12:00am

      Originally posted by: Rajat

      Thanks a lot. needed this info that too presented in such a simple manner.

      Reply
    • Thank you!

      Posted by Legacy on 08/27/2002 12:00am

      Originally posted by: Bill Holman

      Thank you, thank you, thank you!

      I needed a quick way to programatically disable/enable CButton controls, couldn't remember how, went through three MFC books, and the MSDN online help with no success.

      This was an easy fix. I really appreciate it.

      Bill Holman
      Software Systems Engineer

      Reply
    • Another possibility?

      Posted by Legacy on 04/24/2002 12:00am

      Originally posted by: IanH

      How about one function:

      if ((m_bButton) | (m_bEdit) | (m_bCombo))
      {
      GetDlgItem( IDC_BUTTON1 )->EnableWindow( radio_button value );

      ...

      }

      This way you have one function and it does it all.

      Reply
    • How about this?

      Posted by Legacy on 04/21/2002 12:00am

      Originally posted by: Chunliang Pan

      It is a new idea. But every time when you want to enable or
      
      disable a button you still need to set the proper bit (e.g.
      m_bitControls[COMBO_TWO] = FALSE;) and call
      enableORdisableCtrls();

      Could it be easier to use a MACRO? For example, if you

      #define Enable(x) GetDlgItem(x)->EnableWindow(TRUE)
      #define Disable(x) GetDlgItem(x)->EnableWindow(FALSE)
      then you can simply do like this:

      Enable(COMBO_TWO);
      Disable(EDIT_TWO);

      If you really want to use bits to keep tracking the
      status of buttons, you can simply use the really bits, say,
      WORD or DWORD and define the controls like this:

      #define BUTTON_ONE 0X8000 //1000 0000 0000 0000
      ....

      Reply
    Leave a Comment
    • Your email address will not be published. All fields are required.

    Top White Papers and Webcasts

    • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

    • Live Event Date: November 13, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT APIs can be a great source of competitive advantage. The practice of exposing backend services as APIs has become pervasive, however their use varies widely across companies and industries. Some companies leverage APIs to create internal, operational and development efficiencies, while others use them to drive ancillary revenue channels. Many companies successfully support both public and private programs from the same API by varying levels …

    Most Popular Programming Stories

    More for Developers

    Latest Developer Headlines

    RSS Feeds