?
Solved

Getting all controls in a dialog

Posted on 2003-02-19
6
Medium Priority
?
1,421 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

 
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

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

752 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