Link to home
Start Free TrialLog in
Avatar of cranbo
cranbo

asked on

Getting all controls in a dialog

Is there any way I can get a list of all the controls I place on a CDialog so that I can iterate through all of them and perform some action on them?
Avatar of Ess
Ess

One way is to change the way you are creating the controls in the Dialog.  I assume by your question that you do not have variables for your controls, but rather only variables for the values of the controls.  In the class wizard choose member variables and your CDialog derived class.  Select the ID for your control.  Choose Add variable.  Give the variable a name.  In the Category Listbox choose Control rather than Value.  Your control member variable will be a type of the control(e.g. CListbox for a listbox control)rather than the value of the control.  Do this for each control.  You may also want variables for the values for data exchange.

Another solution is to use the API function ::EnumChildWindows.  You will need to specify a procedure to receive the handles to these child windows(the controls).  The problem with this method is that you do not know what type of control is returned. See API documentation for further info.

Hope this helps.
Trying using EnumChildControls.

The function uses a callback function which is called once for each control that's enumerated.
- return FALSE in the callback if you're finished enumerating controls
- return TRUE to continue on to the next control
- place the callback in your dialog's .cpp file.

You can get the type of control by using the GetClassName function.

The following is an example on how to change all button text in a dialog to "What?"

BOOL CALLBACK EnumChildControls(HWND hWnd, LPARAM lParam)
{
     // Make sure window is valid
     if (hWnd && IsWindow(hWnd))
     {
          CWnd* pWnd = CWnd::FromHandle(hWnd);
          ASSERT_VALID(pWnd);

          // Get the class name for the window
          TCHAR szClassName[100];
          GetClassName(hWnd, szClassName, 100);

          TRACE(_T("Class Name: %s\n"), szClassName);

          // Change text on all buttons to What?
          if (_tcscmp(_T("Button"), szClassName) == 0)
          {
               CButton* pButton = (CButton*)pWnd;
               pButton->SetWindowText(_T("What?"));
          }
     }

     return TRUE;
}

void CTestDlgDlg::OnButton2()
{
        // Enumerate the controls
     EnumChildWindows(GetSafeHwnd(), EnumChildControls, 0);
}
Sorry, in the first line of the last message I meant EnumChildWindows, not EnumChildControls.  EnumChildControls is the name of the callback function in my code.

The code is correct.
Oops!  Sorry about forgetting about using GetClassName to get the control type in ::EnumChildWindows callback function.
ASKER CERTIFIED SOLUTION
Avatar of nik2k
nik2k

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 cranbo

ASKER

I accepted nik2k as all I want to do is enable/disable the controls so I only need the CWnd* for each and the answer is very concise.  Perhaps I should have explained that..   Anyway, many thanks to all!