I’m trying to add and delete rows to a GridBagSizer dynamically. Each row of the sizer has a collection of widgets with the rightmost being a ‘delete’ button which removes the row it’s in when pressed. Another button outside the sizer adds a new bottom row of widgets to the sizer when pressed.
I have a simple example app that works, but it’s rather baroque and I’m hoping there’s a simpler way.
The working example detaches and destroys all widgets in the row being deleted, but this doesn’t remove the blank row where the widgets used to be, even after sizer.Layout(). What I have done is detach all widgets in rows below the removed row and move them one row up!?
I'm trying to add and delete rows to a GridBagSizer dynamically. Each
row of the sizer has a collection of widgets with the rightmost being a
'delete' button which removes the row it's in when pressed. Another
button outside the sizer adds a new bottom row of widgets to the sizer
when pressed.
I have a simple example app that works, but it's rather baroque and I'm
hoping there's a simpler way.
The working example detaches and destroys all widgets in the row being
deleted, but this doesn't remove the blank row where the widgets used to
be, even after sizer.Layout(). What I have done is detach all widgets in
rows below the removed row and move them one row up!?
It works, but is there a better way?
There isn't any need to destroy the widgets (other than the row you are getting rid of). Instead just change their row in the sizer. The wxGridBagSizer has a SetItemPosition(item, (row,col)) method that will move the item to a new cell if there isn't already something there.
Another approach that may work for you, depending on your application's organization and needs, is to just move the widget values up to the corresponding widget on the row above and clear the values of the widget on the bottom row. If you have a good separation between the Ui and your data model then this isn't very hard at all.
I wasn’t aware of the SetItemPosition() method. I was just Detach()ing and reinserting, which is probably what SetItemPosition() does underneath anyway, but the method is cleaner.
Thanks,
Ross
···
On Thursday, September 26, 2013 5:00:06 AM UTC+9:30, Robin Dunn wrote:
rzzzwilson wrote:
Hi All,
I’m trying to add and delete rows to a GridBagSizer dynamically. Each
row of the sizer has a collection of widgets with the rightmost being a
‘delete’ button which removes the row it’s in when pressed. Another
button outside the sizer adds a new bottom row of widgets to the sizer
when pressed.
I have a simple example app that works, but it’s rather baroque and I’m
hoping there’s a simpler way.
The working example detaches and destroys all widgets in the row being
deleted, but this doesn’t remove the blank row where the widgets used to
be, even after sizer.Layout(). What I have done is detach all widgets in
rows below the removed row and move them one row up!?
It works, but is there a better way?
There isn’t any need to destroy the widgets (other than the row you are
getting rid of). Instead just change their row in the sizer. The
wxGridBagSizer has a SetItemPosition(item, (row,col)) method that will
move the item to a new cell if there isn’t already something there.
Another approach that may work for you, depending on your application’s
organization and needs, is to just move the widget values up to the
corresponding widget on the row above and clear the values of the widget
on the bottom row. If you have a good separation between the Ui and
your data model then this isn’t very hard at all.