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
Experienced Solver
Experienced Solver
Posts: 81
Joined: Mon Dec 26, 2016 5:21 am
Contact:

dynamic events for check boxes

Post by pvn » Tue Aug 04, 2020 1:23 am

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 179 times

New Pagodi
Super wx Problem Solver
Super wx Problem Solver
Posts: 361
Joined: Tue Jun 20, 2006 6:47 pm
Contact:

Re: dynamic events for check boxes

Post by New Pagodi » Tue Aug 04, 2020 2:06 am

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
Experienced Solver
Experienced Solver
Posts: 81
Joined: Mon Dec 26, 2016 5:21 am
Contact:

Re: dynamic events for check boxes

Post by pvn » Tue Aug 04, 2020 3:14 am

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