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.
A problem about wxCommandEvent? Topic is solved
-
- Earned some good credits
- Posts: 118
- Joined: Sun Aug 28, 2005 8:42 am
-
- Earned some good credits
- Posts: 118
- Joined: Sun Aug 28, 2005 8:42 am
-
- Earned some good credits
- Posts: 118
- Joined: Sun Aug 28, 2005 8:42 am
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
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
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/
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)
2. Create all buttons with the same id and use an event macro like
Code: Select all
EVT_BUTTON(THE_ID, MyFrame::MyHandler)
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
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
-
- Earned some good credits
- Posts: 118
- Joined: Sun Aug 28, 2005 8:42 am