Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a widget by id in wxWidgets?

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.

like image 239
Carcamano Avatar asked Jun 14 '11 02:06

Carcamano


2 Answers

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.

like image 101
SteveL Avatar answered Nov 11 '22 13:11

SteveL


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.

like image 40
rve Avatar answered Nov 11 '22 13:11

rve