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++)

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++)

Editors IDEsSystem ProgrammingC++

Avatar of undefined
Last Comment

8/22/2022 - Mon

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

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) {

Not elegant, but very easy.

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!!
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes