dynamic events for check boxes

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
pvn
Earned some good credits
Earned some good credits
Posts: 105
Joined: Mon Dec 26, 2016 5:21 am
Contact:

dynamic events for check boxes

Post by pvn »

I have a dialog that is filled with check boxes as in image

the number of boxes varies each time, and I need to find on close which ones are checked

normally this is done by finding the window by Id , like 'ID_CHECKBOX_1' in

Code: Select all

wxCheckBox* const cb = (wxCheckBox*)FindWindow(ID_CHECKBOX_1);
  if (cb->IsChecked())
but since the number of boxes varies, it's not possible to assign constant ID values.
Is there a way to solve this ?
Attachments
Untitled.png
Untitled.png (12.97 KiB) Viewed 486 times
New Pagodi
Super wx Problem Solver
Super wx Problem Solver
Posts: 466
Joined: Tue Jun 20, 2006 6:47 pm
Contact:

Re: dynamic events for check boxes

Post by New Pagodi »

You can iterate over the dialogs children and check which ones are check boxes like this:

Code: Select all

wxWindowList children = dialog->GetChildren();

for ( auto it = children.begin() ; it != children.end() ; ++ it )
{
    wxCheckBox* c = wxDynamicCast(*it,wxCheckBox);

    if ( c )
    {
	// Do something with with the check box here.
    }
}
    
(Obviously change "dialog" to the name you're using for the dialog window).
pvn
Earned some good credits
Earned some good credits
Posts: 105
Joined: Mon Dec 26, 2016 5:21 am
Contact:

Re: dynamic events for check boxes

Post by pvn »

thank you

I ended up doing something simpler
on insertion

Code: Select all

wxWindowID ID = ID_LAST + 1;
for (int idx = 0; idx < ts.LUT.size(); idx++)
  {
    wxCheckBox* cb = new wxCheckBox(panel, ID++, ts.LUT.at(idx).name);
and on OK

Code: Select all

wxWindowID ID = ID_LAST + 1;
  for (int idx = 0; idx < ts.LUT.size(); idx++)
  {
    wxCheckBox* const cb = (wxCheckBox*)FindWindow(ID);
    if (cb->IsChecked())
where ID_LAST is the last ID of the other constant window IDs
Post Reply