Click here to Skip to main content
11,411,494 members (67,080 online)
Click here to Skip to main content

Enabling or Disabling controls in more effective and efficient way

, 16 Apr 2002
Rate this:
Please Sign up or sign in to vote.
Using the STL bitset class to create an elegant solution to a common problem
<!-- Download Links --> <!-- Article image -->

Sample Image - screen.jpg

<!-- Add the rest of your HTML here -->

Introduction

Recently I was doing a project in which there are many controls on a dialog box. This dialog box can be display on different user interaction and at different interactions some controls of this dialog box become enabled and some become disabled.

To do this, I have to take decision based on some variable as to which controls are going to be enabled or disabled. 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);
}

Above solution is fine but there are two obvious issues in using the above solution:

  1. Code Replication : There is lot of replication of code that enable or disable various controls. 
  2. Code Maintenance: If in future a control is added then we have to update its state in all the functions in which we are enabling or disabling the controls. Chance is that we might forget to update it at some places.

More effective solution to this problem can be achieved by using  the bitset class of STL. Using the bitset class we can save a sequence consisting of a number of bits, and it 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 which are going to be enable or disable when user click the corresponding radio buttons. Initially all the controls are disabled. When user click radio button labeled "Enable Buttons" then Buttons are going to be enabled and other controls will remain disabled. Other radio buttons will work in the similar manner. Now 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 other two handlers see the code of this article.

This technique provide solutions for two above mentioned issues:

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Jamil Ahmed

United States United States
No Biography provided

Comments and Discussions

 
GeneralI must be missing something too... PinmemberMike Danberg19-Mar-04 16:36 
QuestionAm I missing something? PinsussAnonymous31-Oct-02 9:12 
AnswerRe: Am I missing something? PinmemberTim Smith31-Oct-02 9:38 
GeneralRe: Am I missing something? PinsussAnonymous31-Oct-02 18:51 
AnswerRe: Am I missing something? Pinmembersnyp5-Jul-03 5:11 
Questionwhy not more general way? Pinmemberreal name17-Apr-02 23:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150414.5 | Last Updated 17 Apr 2002
Article Copyright 2002 by Jamil Ahmed
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid