Link to home
Start Free TrialLog in
Avatar of RWoodring
RWoodring

asked on

Need to disable all controls in MFC Dialog

I have a dialog based MFC application (Visual Studio 2005) which serves as a user interface for a piece of equipment.  Before any of the controls in the user interface are useful, the application must connect to the equipment through the network (using TCP sockets).  To support this, I provided a CEdit box for a hostname, CEdit box for a port number, a CButton to connect and CButton to disconnect.  These controls are on the main dialog along with all the controls that actually send information to the equipment.  

What I want to do is to have a simple function which can disable every control in the dialog box and another one that will enable every control.  My OnInitDialog() function would first disable ALL the controls and then enable the hostname, port, and connect button.  After the user clicks the connect button and the network connection is established, ALL controls would be enabled and a few (like the connect button) would be disabled.  I could do this with a long list of calls to EnableWindow, one for each control in my user interface, but I would like to make the enable/disable functions more generic.  

I came up with 2 options for this, but do not know how to implement either one:

1) Is there some way I can get a list of IDC_ control ids dynamically at run time to step through in a loop like the following

List idList = SomeFunction()                  // Get a list of control objects
for(int k = 0; k < idList.length(); k++)
{
      GetDlgItem(k.id)->EnableWindow(false);
}

2) The other option I though of is that if all my IDC_ ids increment, I would only need to know the total number of items and I could do the following:

unsigned int cnt = SomeFunction();            // Get the total number of control objects
for(int k = 0; k < cnt; k++)
{
      GetDlgItem(k)->EnableWindow(false);
}


ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wayside
wayside

You don't have to live with the values of the resource ids that are given to you be the resource editor. You can edit resource.h and make them be whatever you want.

So it is quite easy to make them be consecutive so that you can iterate over them with a for loop. You can even add other values to the file, such as IDC_MIN and IDC_MAX values that you can use for the iteration.

It is a small amount of work to keep resource.h in shape, but dialogs generally don't change that much once they are finished.

You really don't even need a _MAX, just pick some number larger than the number of controls you are likely to have and do something like

for (int i = IDC_MIN; i < IDC_MIN + 20; i++) {
  CWnd *pWnd = GetDlgItem(i);
  if (pWnd) {
    pWnd->EnableWindow(FALSE);
  }
}

Not elegant, but very easy.
Avatar of RWoodring

ASKER

This is exactly what I needed.  I am just not an MFC guy, so functions like EnumChildWindows() and GetSafeHwnd()  are foreign to me.   Thanks for the speedy answer!!