I'm quite new to C++ and wxWidgets.
wxTextCtrl* text = new wxTextCtrl(panel, SOME_ID);
My question is how to get that text control by its ID. I would like to change its value in a different scope. Do I need to keep a pointer to each widget that could have its state changed or is there a way to get that pointer from its ID?
This is probably an easy question, but I guess I couldn't find the correct search terms to find an answer.
You probably want the static function wxWindow::FindWindowById. It returns a plain wxWindow pointer so you will have to cast it to a wxTextCtrl pointer for your example.
Are you sure you want to fetch the text control by it's ID from a different scope?
I think a cleaner solution is to create a Window/Dialog class containing the wxTextCtrl. Then this new class should have a pointer to the wxTextCtrl it contains. Then add a member function to the Window/Dialog class for setting the text. Something like:
class MyWindow: public wxWindow
{
public:
void setTextCtrlText(const wxString &str) { m_textCtrl->ChangeValue(str); };
private:
wxTextCtrl *m_textCtrl;
};
This way you do not have to find a control by ID, you don't have to cast and you do not have to use the ID for the text control in a different scope.
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