Page 1 of 1

A problem about wxCommandEvent?

Posted: Wed Feb 07, 2007 1:21 am
by candid_1120
Hello everyone,I have a trouble that how to do like this:

To build some wxButton and Connect its event differently

I don't know how to solve it,isn't it using the array of pointer

of function? Thanks a lot.

Posted: Wed Feb 07, 2007 1:24 am
by Auria
I don't understand what is the question

Connect an event differently... what does that mean?

Posted: Wed Feb 07, 2007 2:37 am
by candid_1120
That meaning is that I want to build some button with wxButton and use

the member function Connect of wxEvtHandler to implement dynamic

event,but I don't know how to declare the function of event,what about

use array of function of pointer?

Posted: Wed Feb 07, 2007 2:49 am
by candid_1120
more information:

this is part of my code:

wxButton **mn_button = new wxButton*[10];

That can build 10 buttons on the frame;

I want to show different message when I clicked one of the 10 buttons,

how to solve it? Thanks!

Posted: Wed Feb 07, 2007 7:56 am
by upCASE
Hi!
First of all I guess there must be more code than the one you posted. Your code creates an array of ten pointers to wxButton, not the buttons itself.

To tackle your problem, first let me see if I understood this correctly: You want ten buttons and each of the buttons should react differently when clicking them, correct?

There are several way to solve this.
1. Create the buttons with a unique id for each one.
2. Create all buttons with the same id.
3. Create an event handler for each button.
3a. Use the macros to connect them
3b. Use Connect to connect them.

It depends a bit on what you really want to do. If it's only about showing a different message for each, 1 & 2 would be the best options.

1. You could define an id range for the ten button. Loop through the ten and create the buttons and pass them an ascending id from this range. Then create an event handler and an event macro with

Code: Select all

EVT_COMMAND_RANGE(ID_START, ID_END, wxEVT_COMMAND_BUTTON_CLICKED, MyFrame::MyHandler)
This will make MyFrame::MyHandler() receive all click events for buttons with ids between ID_START and ID_END. Use wxCommandEvent::GetId() to get the Id that send the event and show your message accordingly.

2. Create all buttons with the same id and use an event macro like

Code: Select all

EVT_BUTTON(THE_ID, MyFrame::MyHandler) 
To determine the button that was clicked is not that easy now, but it could be with wxEvent::GetEventObject() and some dynamic casting.

3. You would have to create ten event handler functions.
3a. You create the ten buttons, each with a unique id and use the EVT_BUTTON macro to connect each id to a special event handler.
3b. You create the buttons and call Connect on each of them, passing the event handler you want to be called.

Note that you can call Connect() for 1 & 2 as well.

Solution 3 is not the best approach IMHO. 1 or 2 should be the most fitting for you. I'd personally go with one, as this gives you one event handler and an easy way to distinguish between the buttons.

Hope this helps a little.
BTW: There was a blog entry recently about using Connect(). Read it here http://wxwidgets.blogspot.com/

Posted: Thu Feb 08, 2007 3:31 am
by candid_1120
Thanks a lot, I have tried it,that was solved.