Iterate over a large number of controls in MFC

Hi all,

I have a form with about 50 buttons on it. They have all been sub classed into a CJingleButton class. Now when a user clicks on a certain button (not a CJingleButton one) I need to run a method on all 50 buttons. So instead of writing out 50 calls, is there a neat way to put this into some sort of loop?

eg,
1
2
3
4
for (int i=0; i < 50; i++)
{
jingleButton[i].SomeFunction();
}


I did find a function in MFC called EnumChildWindows but this will only pass you the handle to the window, I then am unsure how to know whether this is a button (CButton), a JingleButton (CJingleButton) and how to get the class for the button so I can execute the method.

Many thanks for your help!
Matt
One way is to maintain a collection of these object. If the collection is called jingleButton, you can call your code above when you want them to do something.

BTW, that's a lot of buttons!
Hi kbw,

Many thanks for the reply! So perhaps using something like std::vector<CJingleButton*> and then pushing back the addresses of all the buttons in ram?

Thanks,
Matt
Last edited on
That'll do it. You'll need to tweak to call to jingleButton[i]->SomeFunction();
Yep that did it! Thanks kbw!
Topic archived. No new replies allowed.