Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Getting all controls in a dialog

Posted on 2003-02-19
6
Medium Priority
?
1,634 Views
Last Modified: 2013-11-20
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?
0
Comment
Question by:cranbo
6 Comments
 
LVL 1

Expert Comment

by:Ess
ID: 7984275
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.
0
 
LVL 1

Expert Comment

by:jackrabbit22
ID: 7984372
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);
}
0
 
LVL 1

Expert Comment

by:jackrabbit22
ID: 7984378
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.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Expert Comment

by:Ess
ID: 7984561
Oops!  Sorry about forgetting about using GetClassName to get the control type in ::EnumChildWindows callback function.
0
 

Accepted Solution

by:
nik2k earned 150 total points
ID: 7985574
Somewhat simlier (less lines of code) method is just doing something like this.

MFC version (pDlg is a pointer to your CDialog window):

for(CWnd* pWnd = pDlg->GetWindow(GW_CHILD); pWnd!=NULL; pWnd = pWnd->GetWindow(GW_HWNDNEXT))
{
  // Do your processing for each pWnd here
}

Non-MFC version (hDlg is your dialog window handle)

for(HWND hWnd = GetWindow(hDlg, GW_CHILD); hWnd!=NULL; hWnd = GetWindow(hWnd, GW_HWNDNEXT))
{
  // Do your processing for each hWnd here
}

0
 

Author Comment

by:cranbo
ID: 7986895
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!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses
Course of the Month12 days, 3 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question