Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove controls wxWidgets

Tags:

c++

wxwidgets

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

like image 230
Bushes Avatar asked Jan 01 '13 16:01

Bushes


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

like image 158
Anonymous Coward Avatar answered Sep 19 '22 06:09

Anonymous Coward


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

like image 45
ravenspoint Avatar answered Sep 21 '22 06:09

ravenspoint