Access controls in child wxPanel Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
apoc333
In need of some credit
In need of some credit
Posts: 9
Joined: Thu Dec 07, 2006 1:06 pm
Contact:

Access controls in child wxPanel

Post by apoc333 »

Hi,

this is probably a very silly question but I can't figure out. I'm new to wxWidgets and have understood how events work. Now I want to access a wxTextCtrl from my main frame class. The object ist placed in an other class like wxPanel -> wxNotebook -> wxPanel(as notebook page) -> wxTextCtrl. Any help is very appreciated ;-)

Regards Apoc
Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Post by Jorg »

Hi,

Three approaches. If you are the one creating the controls, store them as a member variable in your class like;

Code: Select all

class MyFrame : public wxFrame
{
private:
   m_button *wxButton;
};

// and further down the road at creation:

m_button = new wxButton(...);
Or you could use FindWindowById, where you give the ID of the window and get a wxWindow pointer back. This can be (dynamically) casted to wxButton or any control you like, but has a potential hazard in the fact you can mis-cast the control, or the cast can return 0.

See
http://www.wxwidgets.org/manuals/2.6.3/ ... windowbyid

The last method is iterating over the wxPanel which will return all the wxWindow * elements on that panel. Only useful when you do not know what is on the wxPanel and you need to scan for the controls.

Regards,
- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
apoc333
In need of some credit
In need of some credit
Posts: 9
Joined: Thu Dec 07, 2006 1:06 pm
Contact:

Post by apoc333 »

Hi Jorg, thanks for the input!

I was playing arround with FindWindowById but didn't have in mind that wxTextCtrl is derived from wxWindow. Now I have a working solution. Is this the way to go or is there a better one?

Code: Select all

	wxTextCtrl* myTC;
	myTC = (wxTextCtrl*)this->FindWindowById(ID_MySQLADDRESS);
	wxMessageBox(myTC->GetValue());
Regards Apoc
S.Volkenandt
Knows some wx things
Knows some wx things
Posts: 26
Joined: Tue Nov 14, 2006 1:51 pm
Location: Duesseldorf, Germany

Post by S.Volkenandt »

You should use a C++ style cast:

Code: Select all

wxTextCtrl* myTC = static_cast< wxTextCtrl* >(FindWindowById(ID_MySQLADDRESS)); // if you are _absolutely_ sure that FindWindowById(ID_MySQLADDRESS) will return a wxTextCtrl*
if (myTC == 0) { /* nope, this control was not found */ }
// or
wxTextCtrl* myTC = dynamic_cast< wxTextCtrl* >(FindWindowById(ID_MySQLADDRESS));
if (myTC == 0) { /* nope, this control was either not found or not a text control */ }
Post Reply