| Forum List • Thread List • Reply • Refresh • New Topic • Search • Previous • Next 1 | 1. Event Tables (in different classes) #4888 Posted by: 2004-07-23 03:23:11 | Hello,
I know how to work the event tables that are in the native class and it works fine but this is a bit different. I have 3 classes which creates the pages that go in a Notebook using wxPanel and has simply a set of buttons. In the main frame class I declare and define the notebook and add the pages. I need to have the functions each button is attached to in the main frame class.
This is what I have in the main frame class:
// IDs for the controls and the menu commands enum { ID_PG1_BUTTON1 = 0, ID_PG1_BUTTON2, ID_PG1_BUTTON3,
ID_PG2_BUTTON1, ID_PG2_BUTTON2, ID_PG2_BUTTON3,
ID_PG3_BUTTON1, ID_PG3_BUTTON2, ID_PG3_BUTTON3, }
BEGIN_EVENT_TABLE(wxPage1, wxPanel) EVT_BUTTON(ID_PG1_BUTTON1 , MainFrame::OnPg1button1) EVT_BUTTON(ID_PG1_BUTTON2 , MainFrame::OnPg1button2) EVT_BUTTON(ID_PG1_BUTTON3 , MainFrame::OnPg1button3) END_EVENT_TABLE()
//same for wxPage2 and wxPage3
BOOL MainFrame::OnPg1button1(wxCommandEvent& WXUNUSED(event)) { return(foo()); } BOOL MainFrame::OnPg1button2(wxCommandEvent& WXUNUSED(event)) { return(foo()); } BOOL MainFrame::OnPg1button3(wxCommandEvent& WXUNUSED(event)) { return(foo()); }
//same for OnPg2... and OnPg3button...
The function foo() has to be in the main frame class because it uses some of its objects.
Problem: Now if I set ID_PG1_BUTTON1 to 0, when I click the first button, nothing happens but when I click on the second one, the function that was attached to the first one is activated. If I set ID_PG1_BUTTON1 to 1, the functions are pushed of two buttons. If I set it to -1, all buttons have the same functionality as the first one. If I set it to a random number or 1000, nothing happens when I click on the buttons
So I cannot seem to get the right function for the right button!! What I understand from the enum is that an ID is set to a certain number and in the event table list the ID# is attached to the function and the button. It is surely a newbie question, sorry if the question is really simple ;)
Thx,
moons
| 2. Re: Event Tables (in different classes) #4893 Posted by: upCASE 2004-07-23 15:07:42 | Hi! "What I understand from the enum is that an ID is set to a certain number" Kind of :) If you specify the first number, the rest is enumerated. Starting with 0 may not be a good idea. Try
enum{ ID_FIRST = wxID_HIGHEST+1, ID_NEXT }
"in the event table list the ID# is attached to the function and the button" Kind of, too :) Each button gets a (normally) unique ID when contructed. The event table entry now tells wxWidgets, that when a certain event to emitted (e.g. wxCommandEvent) to check the ID of the generating widget (e.g. a button). If the event and the ID match the function is called. Otherwise (if the event is allowed to propagate) the next event table is searched (normally the one of the parent window). Now, having the same IDs for different widgets may have undesired side-effects, like a function beeing called two times. Make sure the IDs are unique and that you assign the right IDs to the correct widgets.
upCASE ----------------------------------- If it was hard to write, it should be hard to read!- Do. Or do not. There is no try! | 3. Re: Event Tables (in different classes) #4894 Posted by: 2004-07-23 21:21:49 | Hi,
I tried setting the first one to wxID_HIGHEST+1 and no buttons work now. I suspect it has something to do whith the fact that I declare the enum for the IDs and the event tables in a separate class of where the buttons are initiated. I still don't understand why it works when I set the first ID to 0 (well, all functions shifted by one button). Any other ideas?, I'm really stuck ;)
Thx
moons | 4. Re: Event Tables (in different classes) #4895 Posted by: 2004-07-23 21:28:31 | Hi,
Thx upCase, solved it...I wasn't setting the IDs in the buttons the same as the IDs in the enum so it wasn't sticking the right function to the right button. It works fine now, thx again for your tremendous help ;)
moons | Forum List • Thread List • Reply • Refresh • New Topic • Search • Previous • Next 1 |
|
|