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
2 Answers
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).

- 6,186
- 1
- 23
- 29
-
1It is dangerous to simply call delete on the pointer. You might end up with messages being sent to a deleted window. It is better to call Destroy(). – ravenspoint Jan 01 '13 at 19:56
-
@ravenspoint You are correct. Some controls perform additional cleanup in `Destroy()` which is not done by the destructor. – Anonymous Coward Jan 01 '13 at 21:18
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

- 19,093
- 6
- 57
- 103