A problem about wxCommandEvent? 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
candid_1120
Earned some good credits
Earned some good credits
Posts: 118
Joined: Sun Aug 28, 2005 8:42 am

A problem about wxCommandEvent?

Post 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.
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

I don't understand what is the question

Connect an event differently... what does that mean?
candid_1120
Earned some good credits
Earned some good credits
Posts: 118
Joined: Sun Aug 28, 2005 8:42 am

Post 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?
candid_1120
Earned some good credits
Earned some good credits
Posts: 118
Joined: Sun Aug 28, 2005 8:42 am

Post 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!
upCASE
Moderator
Moderator
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Post 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/
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4

"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda
candid_1120
Earned some good credits
Earned some good credits
Posts: 118
Joined: Sun Aug 28, 2005 8:42 am

Post by candid_1120 »

Thanks a lot, I have tried it,that was solved.
Post Reply