OVERVIEW
We now take the sample from the previous section and implement the event handling using Connect instead of an Event table
SAMPLE CODE
events-connect.h
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
class EventsConnect: public wxFrame {
public:
EventsConnect(const wxString& title);
void OnClick(wxCommandEvent &event);
void OnText(wxCommandEvent &event);
private:
wxStaticText *m_lbl;
wxTextCtrl *m_edit;
};
const int ID_BTN_1 = 101;
const int ID_BTN_2 = 102;
const int ID_BTN_3 = 103;
const int ID_TXT_1 = 104;
events-connect.cpp
#include "events-connect.h"
EventsConnect::EventsConnect(const wxString& title):
wxFrame(NULL, -1, title, wxDefaultPosition, wxSize(500,200)) {
wxPanel *panelMain = new wxPanel(this, -1);
wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
panelMain->SetSizer(vbox);
wxPanel *panelBase = new wxPanel(panelMain, -1);
wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);
panelBase->SetSizer(hbox);
wxButton *btn1 = new wxButton(panelBase, ID_BTN_1, wxT("Button 1"));
wxButton *btn2 = new wxButton(panelBase, ID_BTN_2, wxT("Button 2"));
wxButton *btn3 = new wxButton(panelBase, ID_BTN_3, wxT("Button 3"));
m_edit = new wxTextCtrl(panelBase, ID_TXT_1, wxT(""));
hbox->Add(btn1, 1, wxALL,5);
hbox->Add(btn2, 1, wxALL,5);
hbox->Add(btn3, 1, wxALL,5);
hbox->Add(m_edit, 2, wxALL,5);
vbox->Add(panelBase,1, wxALL,2);
m_lbl = new wxStaticText(panelMain, -1, wxT("show event handling here"));
vbox->Add(m_lbl, 1, wxALL | wxALIGN_CENTER);
Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(EventsConnect::OnClick));
Connect(wxEVT_TEXT, wxCommandEventHandler(EventsConnect::OnText));
Center();
}
void EventsConnect::OnClick(wxCommandEvent &event) {
wxString strId = wxString::Format(wxT("%d"), event.GetId());
strId.Append(wxString(" clicked"));
m_lbl->SetLabel(strId);
}
void EventsConnect::OnText(wxCommandEvent &event) {
m_lbl->SetLabel(m_edit->GetValue());
}
main.h
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
class Main: public wxApp {
public:
virtual bool OnInit();
};
main.cpp
#include "main.h"
#include "events-connect.h"
IMPLEMENT_APP(Main)
bool Main::OnInit() {
EventsConnect *app = new EventsConnect(wxT("Events Connect"));
app->Show(true);
}
The output is shown below
Leave a Reply