There is the main frame (previewFrame) and a class that inherits from wxArrangeCtrl.
When I select an item in the list I want to fire an event that will display the properties of the selected item.
The event fires but the result is strange.
I have removed most of the code to show what is happening:
Code: Select all
class RenderList : public wxRearrangeCtrl {
public:
// constructor etc ....
// .......
// bind a function to the wxListctrl event
void bindEvent(int eventType, wxObjectEventFunction onEvent) {
int listID = list->GetId();
Connect(listID, eventType, onEvent);
}
}
class previewFrame: public wxFrame {
public:
// constructor etc ....
// .......
RenderList * renderers;
int count = 10;
void fillProperties(int idx) ;
void OnRendererSelect(wxCommandEvent& event);
}
// the main frame constructor
previewFrame::previewFrame(wxWindow* parent,wxWindowID id) {
Create(parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("wxID_ANY"));
// create the renderlist (wxRearrangeCtrl)
renderers = new RenderList(scrollerTop, ID_RENDERLIST, wxPoint(0,0), wxSize(350,200));
// bind OnRendererSelect() to the listCtrl from renderers
renderers->bindEvent(wxEVT_COMMAND_LISTBOX_SELECTED,
(wxObjectEventFunction)&previewFrame::OnRendererSelect);
}
// called by onselect event from Renderlist::wxListCtrl
void previewFrame::OnRendererSelect(wxCommandEvent& event) {
int idx = event.GetInt(); // the index of the list item that is clicked
fillProperties(idx); // from the previewFrame class - no problems
}
// variable a has a random value - there is no error, it is just random
void previewFrame::fillProperties(int idx) {
int a = count; // count from the previewFrame class (should be 10)
}
The event fires and calls OnRendererSelect().
OnRendererSelect() calls fillProperties()
fillProperties assigns integer-a to count (member of previewFrame which is initialized as 10)
When I run this code in the debugger it should show that 'a' is 10. However, the value is random.
And now my temporary solution:
I make a global variable (pointer) of the type previewFrame and assign this to the previewFrame that is created.
previewFrame *t;
In the previewFrame constructor:
t = this;
Then in fillProperties():
int a = t->count;
this solves the problem but it is far from elegant.
What is going on and what is my mistake?