I found this quite hard to find from searching but quite simply, how do you remove controls from a panel? I have some wxStaticText and wxTextCtrl and I want to swap delete the existing items and replace them with new ones? Is there some sort of command I can call or do I have to make something myself? Cheers
EDIT: as ravenspoint pointed out, simply deleting the control is not enough. Some controls perform additional cleanup in Destroy()
.
You can simply Destroy()
the control. wxWidgets will automatically remove it from the parent window and free its memory.
wxWindow* ctrl = new wxStaticText(this);
ctrl->Destroy();
ctrl = new wxTextCtrl(this);
If you do not have a pointer to the control, you can use FindWindowById
, FindWindowByLabel
or FindWindowByName
to obtain it:
if(wxWindow* ctrl = wxWindow::FindWindowById(ID_MYCTRL,this))
ctrl->Destroy();
If the control was added to a sizer, it has to be replaced while it is still valid:
newCtrl = new wxWindow(...);
sizer->Replace(oldCtrl,newCtrl); // both oldCtrl and newCtrl must be valid
oldCtrl->Destroy();
Layout(); // update sizer
Alternatively, you could create a wxTextCtrl from the start and make it read-only. However, additional style modifications would be required to make it look like a wxStaticText (for example the background color, border etc).
The simplest thing to do is to hide the widget. http://docs.wxwidgets.org/trunk/classwx_window.html#a7ed103df04014cb3c59c6a3fb4d95328
However, if you want to permanently remove the widget, then call Destroy http://docs.wxwidgets.org/trunk/classwx_window.html#a6bf0c5be864544d9ce0560087667b7fc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With