Page 1 of 1

dynamic events for check boxes

Posted: Tue Aug 04, 2020 1:23 am
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 ?

Re: dynamic events for check boxes

Posted: Tue Aug 04, 2020 2:06 am
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).

Re: dynamic events for check boxes

Posted: Tue Aug 04, 2020 3:14 am
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