How to ref panel objects on event? 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
tuk1
Earned some good credits
Earned some good credits
Posts: 114
Joined: Sun Oct 08, 2017 9:36 am

How to ref panel objects on event?

Post by tuk1 »

On PANEL init, a wxTextCtrl(text input field) object is created:

Code: Select all

void PANEL::Init()
{   
    wxTextCtrl* itemTextCtrl = new wxTextCtrl(
     itemStaticBoxSizer11->GetStaticBox(),
     ID_TEXTCTRL, wxEmptyString, 
     wxDefaultPosition, wxSize(400, -1), 
     wxTE_PROCESS_ENTER 
     );
}
We set an event on the text field...should user press enter key!

Code: Select all

BEGIN_EVENT_TABLE( PANEL, wxDialog )

EVT_TEXT_ENTER( wxID_ANY, PANEL::OnTextctrlEnter )

END_EVENT_TABLE()

And finally, function to call in event of user pressing enter key.

Code: Select all

void PANEL::OnTextctrlEnter( wxCommandEvent& event )
{
     // reference text field data and do stuff!!!
}
-----

What is the best way to reference the wxTextCtrl object? ...for eg to access the contents of the text field?

Will wxTextCtrl need to be declared as a member of the Panel class or?
wxWidgets(v3.2.2.1) - Vs2022(v143) - Win10(x64) - DialogBlocks(v5.16.5_Unicode)
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to ref panel objects on event?

Post by doublemax »

There are several ways to do this:

In your case the best is:

Code: Select all

wxTextCtrl *tc = wxDynamicCast( event.GetEventObject(), wxTextCtrl );
if( tc != NULL )
{
  // do something with the text control
}
You could store the pointer in a member variable, but that wouldn't work if you have one event handler for multiple controls.
Use the source, Luke!
tuk1
Earned some good credits
Earned some good credits
Posts: 114
Joined: Sun Oct 08, 2017 9:36 am

Re: How to ref panel objects on event?

Post by tuk1 »

Thanks!!!
wxWidgets(v3.2.2.1) - Vs2022(v143) - Win10(x64) - DialogBlocks(v5.16.5_Unicode)
User avatar
eranon
Can't get richer than this
Can't get richer than this
Posts: 867
Joined: Sun May 13, 2012 11:42 pm
Location: France
Contact:

Re: How to ref panel objects on event?

Post by eranon »

Just a small add about things you should be aware around your question:

The way doublemax expressed is obviously an usual safe one, but you need RTTI. If you disabled RTTI, dynamic cast will be not allowed (compiler will tell you). So, if your event handler handles event for a known type of control, you can go through a static cast, but you have to be sure at writing time. In this field, if your handler is about one control only, it's better to provide an explicit ID (rather wxID_ANY) in your event table.

In your event handler, you can access any members too, but in this case it's better to take care of event sink to prevent some situations generating crash (SIGFAULT or SIGSEGV).
[Ind. dev. - wxWidgets 3.0/3.1 under "Win 7 64-bit, TDM64-GCC" + "OS X 10.9, LLVM Clang"]
Post Reply