EVT_CHAR in wxNoteBook and wxPanel Topic is solved

If you are using wxDev-C++ for your wxWidgets design, please ask your questions here instead of in IDE Related.
Post Reply
traltixx
Knows some wx things
Knows some wx things
Posts: 32
Joined: Wed Jan 17, 2007 6:00 am

EVT_CHAR in wxNoteBook and wxPanel

Post by traltixx »

Hi,
I have wxPanels that are being added to a wxNoteBook dynamically (by the use of some wxButtons and wxMenuItems).. anyway, I have a EVT_CHAR on the whole application. How to I bind the EVT_CHAR to these wxPanels that are being added to the wxNoteBook? Or how do I bind EVT_CHAR dynamically to other wxWidget objects?
I ask since Im having problem with the EVT_CHAR. Specifically because it does not work unless i click/focus in between the menu bar and the notebook page. I donr understand. Even if i set focus on the wxPanel, the EVT_CHAR does not trigger.
Thanks!
spedgenius
Experienced Solver
Experienced Solver
Posts: 66
Joined: Thu Dec 15, 2005 8:16 pm

Post by spedgenius »

you can dynamically add an event handler with Connect()

Code: Select all

myPanel->Connect(wxID_ANY, EVT_CHAR, wxKeyEventHandler(MyFrame::OnChar));

the Id parameter specefies the id of the event procesessed, in the case of a keyboard event wxID_ANY is used.
the second is the type of event.
the third is the function that the event is mapped to. wxKeyEventHandler() is a macro that wraps the function name.



you could also inherit from wxPanel and override the event handler.

if you post the code that your having problems with we might be able to help more
lowjoel
Part Of The Furniture
Part Of The Furniture
Posts: 1511
Joined: Sun Jun 19, 2005 11:37 am
Location: Singapore
Contact:

Post by lowjoel »

spedgenius wrote:you can dynamically add an event handler with Connect()

Code: Select all

myPanel->Connect(wxID_ANY, EVT_CHAR, wxKeyEventHandler(MyFrame::OnChar));

the Id parameter specefies the id of the event procesessed, in the case of a keyboard event wxID_ANY is used.
the second is the type of event.
the third is the function that the event is mapped to. wxKeyEventHandler() is a macro that wraps the function name.



you could also inherit from wxPanel and override the event handler.

if you post the code that your having problems with we might be able to help more
Your code will crash. It should be

Code: Select all

myPanel->Connect(wxID_ANY, EVT_CHAR, wxKeyEventHandler(MyFrame::OnChar), NULL, this);
because of a difference in the this pointer.

Joel
traltixx
Knows some wx things
Knows some wx things
Posts: 32
Joined: Wed Jan 17, 2007 6:00 am

Post by traltixx »

Thanks for the info and it works fine.
Also, It seems that I cant bind EVT_CHAR to wxButton or wxTextCtrl in the Event Table declarations. Is there an example where I can do this?
thanks!
EDIT: my bad. I can do connect like this to a wxTextCtrl and wxButtons it works fine. Amazing!
spedgenius
Experienced Solver
Experienced Solver
Posts: 66
Joined: Thu Dec 15, 2005 8:16 pm

Post by spedgenius »

joel,
Your code will crash. It should be

Code: Select all

myPanel->Connect(wxID_ANY, EVT_CHAR, wxKeyEventHandler(MyFrame::OnChar), NULL, this); 
because of a difference in the this pointer.

according to the helpfile if the eventSink parameter is left as null, the this pointer will automatically be used instead.
userData
Data to be associated with the event table entry.

eventSink
Object whose member function should be called. If this is NULL, this will be used.
are you saying you have to explicetly pass this because it points to something else if left NULL? if left NULL the function will use a pointer to the instance that connect is called on, in this case would be MyPanel. whereas if you passed this as you suggested is would point to MyFrame. would not both be acceptable? or must it point to the same instance that owns the eventhandler function. ie..MyFrame::OnChar.
lowjoel
Part Of The Furniture
Part Of The Furniture
Posts: 1511
Joined: Sun Jun 19, 2005 11:37 am
Location: Singapore
Contact:

Post by lowjoel »

Yes. It must point to the class which "owns" the member function. Try your own way, you'll crash when the compiler tries to call the function.

Joel
Post Reply