I am new to C and wxWidgets and trying to follow a basic example which appears to run with the exception of the OnButtonClicked
marked by <<<<<<<
The variables are undefined at the Build stage (if I comment out both lines the code will run)
Is there something wrong with the definition of the variable m_list1 and m_txt1?
//===================cMain.h ===========
#pragma once //see 22:450
#include "wx/wx.h"
class cMain : public wxFrame
{
public:
cMain();
~cMain();
public:
wxButton *m_btn1 = nullptr; //25:48
wxTextCtrl *m_txt1 = nullptr;
wxListBox *m_list1 = nullptr;
void OnButtonClicked(wxCommandEvent &evt); //27:30
wxDECLARE_EVENT_TABLE(); // 27:34
};
//===================cMain.cpp ===========
#include "cMain.h"
wxBEGIN_EVENT_TABLE(cMain,wxFrame) // 28:00
//EVT_BUTTON(10001,OnButtonClicked) //28:33
wxEND_EVENT_TABLE()
cMain::cMain() : wxFrame(nullptr, wxID_ANY, "Peter's First WxWidget Code ",wxPoint(30,30),wxSize(800,600))
{
m_btn1 = new wxButton(this, 10001, "Click Me", wxPoint(10, 10), wxSize(150, 50)); //28:36
m_txt1=new wxTextCtrl(this, wxID_ANY, "", wxPoint(10, 70), wxSize(300, 30));
m_list1=new wxListBox(this, wxID_ANY, wxPoint(10, 110), wxSize(300,300));
}
cMain::~cMain()
{
}
void OnButtonClicked(wxCommandEvent &evt)
{
//m_list1->AppendString(m_txt1->GetValue()); //<<<<<<<<<<<<<<<
m_list1->AppendString("ONE"); //<<<<<<<<<<<<<<<
evt.Skip(); //29.24
}
//===================cApp.h ===========
#pragma once //24:09
#include "wx/wx.h"
#include "cMain.h"
class cApp : public wxApp
{
public:
cApp();
~cApp();
private:
cMain* m_frame1 = nullptr;
public:
virtual bool OnInit();
};
//===================cApp.cpp ===========
#include "cApp.h" // 24:19
wxIMPLEMENT_APP(cApp);
cApp::cApp()
{
}
cApp::~cApp()
{
}
bool cApp::OnInit()
{
m_frame1 = new cMain();
m_frame1->Show();
return true;
}
Any help appreciated
CodePudding user response:
void OnButtonClicked(wxCommandEvent &evt)
should have been
void cMain::OnButtonClicked(wxCommandEvent &evt)
CodePudding user response:
Do you mean m_list1
is not found as a valid variable name?
void OnButtonClicked(wxCommandEvent &evt)
is a stand-alone function. It is not part of any class.
The only m_list1
I see is a member of cMain
. So, it is not in the scope of the function. It would be a member variable if you were writing a member function of that class, but you're writing a global (free) function.
You don't have any pointer to any instance of any of the classes you define in your source, so you can't get there from here.
Perhaps you want OnButtonClick
to be a member of cMain
. I don't know this framework, but I see you're not registering the function pointer to a callback; if the framework just knows to call a member of that name, then your mistake was simply not making this function a member of the class.