How can I add event & function to button from other clas 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
aalien
In need of some credit
In need of some credit
Posts: 8
Joined: Sat Jan 27, 2007 6:00 pm

How can I add event & function to button from other clas

Post by aalien »

Hi All

I'm pretty new to wxWidgets, and I have a simple problem with events.
How can I add event and function to button from other than main classs. In code below i marked the problem button
My project looks like that :

Code: Select all

//----------------------------------------BEGIN  stdwx.h----------------------------------------
#ifndef STDWX_H
#define STDWX_H

#include <wx/wxprec.h>
//#include "..\_wx_intellisense.h"
#include <wx/wx.h>
#include <wx/notebook.h>
#include <wx/button.h>
#include <wx/stattext.h>
#include <wx/textctrl.h>
#include <wx/filedlg.h>

#endif
//----------------------------------------END stdwx.h----------------------------------------

//----------------------------------------BEGIN guipageki.h----------------------------------------
#ifndef GUIPAGEKI_H
#define GUIPAGEKI_H


class pageKI : public wxNotebookPage
{
private:
	wxStaticText		*KIstatictextFname;
	wxTextCtrl			*KItextctrlFname;
	wxStaticText		*KIstatictextStream;
	wxTextCtrl			*KItextctrlStream;	
	//_************************************* P R O B L E M    BU T T O N**************************************	
	wxButton     *ex_Button;                 //_HOW ADD EVENT 
	//void            On_ex_Button(wxCommandEvent& event);              //and IT's FUNCTION ETC. FOR THAT BUTTON
	//_**********************************************************************************************

public:
	pageKI(wxWindow * parent, wxString fname = "", wxString stream = "");
};

enum
{
	ex__Button2=11054,
	ex__Button3
};
#endif
//----------------------------------------END guipageki.h----------------------------------------
//----------------------------------------BEGIN guipageki.cpp----------------------------------------
#include "stdwx.h"
#include "guipageki.h"

pageKI::pageKI(wxWindow * parent, wxString fname, wxString stream):wxNotebookPage(parent, wxID_ANY)
{
	KIstatictextFname = new wxStaticText(this, wxID_ANY,_T("nazwa pliku"),wxPoint(10,10));
	KItextctrlFname = new wxTextCtrl(this,wxID_ANY,fname,wxPoint(10,30),wxSize(730,20));
	KIstatictextStream = new wxStaticText(this, wxID_ANY,_T("konfiguracja"),wxPoint(10,50));
	KItextctrlStream = new wxTextCtrl(this,wxID_ANY,stream,wxPoint(10,70),wxSize(730,20));
	//_**********************************************************************************************
	//_HOW ADD EVENT and IT's FUNCTION ETC. FOR THAT BUTTON
	ex_Button = new wxButton(this, ex__Button2, "cuss", wxPoint(100,0));
	//_**********************************************************************************************
}
//----------------------------------------END guipageki.cpp----------------------------------------
//----------------------------------------BEGIN guimain.h----------------------------------------
#ifndef GUIMAIN_H
#define GUIMAIN_H

class MyApp : public wxApp
{
	public:    virtual bool OnInit();
};
class MyFrame : public wxFrame
{
public:
    MyFrame(const wxString& title, int xpos, int ypos, int width, int height);
    void OnQuit(wxCommandEvent& event);
    void OnAbout(wxCommandEvent& event);
	void OnMybutton(wxCommandEvent& event);
private:
	wxNotebook			*scaNotebook;
	wxButton			*KIbuttonLoad;//for that button everything is clear
	wxNotebookPage		*KIpage;
	wxNotebook			*KInotebook;
    DECLARE_EVENT_TABLE()
};

enum
{
    Minimal_Quit = wxID_EXIT,
	sca_notebook,
	KI_page,
	KI_buttonLoad,
	KI_notebook,
    Minimal_About = wxID_ABOUT,
};

BEGIN_EVENT_TABLE(MyFrame, wxFrame)
    EVT_MENU(Minimal_Quit,  MyFrame::OnQuit)
    EVT_MENU(Minimal_About, MyFrame::OnAbout)
	EVT_BUTTON(KI_buttonLoad, MyFrame::OnMybutton)
END_EVENT_TABLE()

#endif
// ---------------------------------------- END guimain.h ----------------------------------------
// ----------------------------------------BEGIN  guimain.cpp ----------------------------------------
#include "stdwx.h"

#if !defined(__WXMSW__) && !defined(__WXPM__)
    #include "../sample.xpm"
#endif
#include "guipageki.h"
#include "guimain.h"

IMPLEMENT_APP(MyApp)

bool MyApp::OnInit()
{
    MyFrame *frame = new MyFrame(_T("SCA gui"), 5,50,800,600 );
    frame->Show(true);
    return true;
}
MyFrame::MyFrame(const wxString& title, int xpos, int ypos, int width, int height)
       : wxFrame(NULL, wxID_ANY, title, wxPoint(xpos, ypos), wxSize(width,height))
{
    SetIcon(wxICON(sample));
#if wxUSE_MENUS
    wxMenu *fileMenu = new wxMenu;    wxMenu *helpMenu = new wxMenu;    helpMenu->Append(Minimal_About, _T("&O programie...\tF1"), _T("Wyświetla informacje o programie"));    fileMenu->Append(Minimal_Quit, _T("E&xit\tAlt-X"), _T("Wyjście z programu"));    wxMenuBar *menuBar = new wxMenuBar();    menuBar->Append(fileMenu, _T("&Plik"));    menuBar->Append(helpMenu, _T("&Pomoc"));    SetMenuBar(menuBar);
#endif // wxUSE_MENUS

#if wxUSE_STATUSBAR
    CreateStatusBar(2);    SetStatusText(_T("Zapraszam!"));
#endif // wxUSE_STATUSBAR

	scaNotebook = new wxNotebook(this,sca_notebook, wxPoint(10,10), wxSize(200,100));
	KIpage = new wxNotebookPage( scaNotebook, KI_page);
	
	KIbuttonLoad = new wxButton(KIpage, KI_buttonLoad, _T("ładuj"), wxPoint( 10, 10), wxSize(100,20));
	KInotebook = new wxNotebook(KIpage, KI_notebook, wxPoint(10,50), wxSize(760,130));

	scaNotebook->AddPage(KIpage,_T("ki"));
}

void MyFrame::OnMybutton(wxCommandEvent& WXUNUSED(event))
{
	wxString filename = wxFileSelector("Wybierz konfiguracje inicjującą");
	if(!filename.empty())
	{
		pageKI	* npki = new pageKI(KInotebook);
		KInotebook->AddPage(npki,"0");
	}
}

void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)){  Close(true);	}
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
    wxString msg;
    msg.Printf( _T("Autorem programu jest Andrzej Wawrzyniak\n")
                _T("\n\tPowered by %s"), wxVERSION_STRING);

    wxMessageBox(msg, _T("O programie SCA"), wxOK | wxICON_INFORMATION, this);
}
// ----------------------------------------END guimain.cpp ----------------------------------------
phlox81
wxWorld Domination!
wxWorld Domination!
Posts: 1387
Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany
Contact:

Post by phlox81 »

you have to call connect in your class with the following parameters:
Connect(button->GetId(),wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(MyFrame::OnButton),0,pointer_to_mainframe);
aalien
In need of some credit
In need of some credit
Posts: 8
Joined: Sat Jan 27, 2007 6:00 pm

Post by aalien »

thank you for your response

i did something like that:

Code: Select all


//----------------------------------------BEGIN PART OF guipageki.cpp----------------------------------------
pageKI::pageKI(wxWindow * parent, wxString fname, wxString stream):wxNotebookPage(parent, wxID_ANY)
{
	KIstatictextFname = new wxStaticText(this, wxID_ANY,_T("nazwa pliku"),wxPoint(10,10));
	KItextctrlFname = new wxTextCtrl(this,wxID_ANY,fname,wxPoint(10,30),wxSize(730,20));
	KIstatictextStream = new wxStaticText(this, wxID_ANY,_T("konfiguracja"),wxPoint(10,50));
	KItextctrlStream = new wxTextCtrl(this,wxID_ANY,stream,wxPoint(10,70),wxSize(730,20));
	//_**********************************************************************************************
	//_HOW ADD EVENT and IT's FUNCTION ETC. FOR THAT BUTTON
	ex_Button = new wxButton(this, ex__Button2, "cuss", wxPoint(100,0));
	this->Connect(ex_Button->GetId(),wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(pageKI::OnButton),0,parent->GetParent());	
	//_**********************************************************************************************
}
//----------------------------------------END PART OF guipageki.cpp----------------------------------------
but i'm getting error, how should i do correctly
  • ------ Build started: Project: gui, Configuration: Debug Win32 ------
    Linking...
    guipageki.obj : error LNK2019: unresolved external symbol "private: void __thiscall pageKI::OnButton(class wxCommandEvent &)" (?OnButton@pageKI@@AAEXAAVwxCommandEvent@@@Z) referenced in function "public: __thiscall pageKI::pageKI(class wxWindow *,class wxString,class wxString)" (??0pageKI@@QAE@PAVwxWindow@@VwxString@@1@Z)
    E:\developing\C\2007_01\2007_01_27_17.33__wxtst\gui\Debug\gui.exe : fatal error LNK1120: 1 unresolved externals
    Build log was saved at "file://e:\developing\C\2007_01\2007_01_27_17.33__wxtst\gui\gui\Debug\BuildLog.htm"
    gui - 2 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
benedicte
wxWorld Domination!
wxWorld Domination!
Posts: 1409
Joined: Wed Jan 19, 2005 3:44 pm
Location: Paris, France

Post by benedicte »

aalien wrote:...
how should i do correctly
...
You need to declare and define the following function:

Code: Select all

void pageKI::OnButton (wxCommandEvent &event) // event may be unused: WXUNUSED(event)
aalien
In need of some credit
In need of some credit
Posts: 8
Joined: Sat Jan 27, 2007 6:00 pm

Post by aalien »

You need to declare and define the following function:

void pageKI::OnButton (wxCommandEvent &event) // event may be unused: WXUNUSED(event)
I did, but application is crashing when I click ex_Button.
Code I modified is marked by '*'

Code: Select all

//----------------------------------------BEGIN guipageki.h----------------------------------------
#ifndef GUIPAGEKI_H
#define GUIPAGEKI_H
class pageKI : public wxNotebookPage
{
private:
	wxStaticText		*KIstatictextFname;
	wxTextCtrl			*KItextctrlFname;
	wxStaticText		*KIstatictextStream;
	wxTextCtrl			*KItextctrlStream;	
	//_************************************* P R O B L E M    BU T T O N**************************************	
	void OnButton(wxCommandEvent& event);
	wxButton     *ex_Button;             
	//_**********************************************************************************************

public:
	pageKI(wxWindow * parent, wxString fname = "", wxString stream = "");
};

enum
{
	ex__Button2=11054,
	ex__Button3
};
#endif
//----------------------------------------END guipageki.h----------------------------------------
//----------------------------------------BEGIN guipageki.cpp----------------------------------------
#include "stdwx.h"
#include "guipageki.h"

pageKI::pageKI(wxWindow * parent, wxString fname, wxString stream):wxNotebookPage(parent, wxID_ANY)
{
	KIstatictextFname = new wxStaticText(this, wxID_ANY,_T("nazwa pliku"),wxPoint(10,10));
	KItextctrlFname = new wxTextCtrl(this,wxID_ANY,fname,wxPoint(10,30),wxSize(730,20));
	KIstatictextStream = new wxStaticText(this, wxID_ANY,_T("konfiguracja"),wxPoint(10,50));
	KItextctrlStream = new wxTextCtrl(this,wxID_ANY,stream,wxPoint(10,70),wxSize(730,20));
	ex_Button = new wxButton(this, ex__Button2, "cuss", wxPoint(100,0));
	//_**********************************************************************************************
	this->Connect(ex_Button->GetId(),wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(pageKI::OnButton),0,parent->GetParent());
	//_**********************************************************************************************
}

void pageKI::OnButton(wxCommandEvent& event)
{
	//_**********************************************************************************************
	// IT CRASH IN ROW BELOW BUT I think it should NOT 
	this->ex_Button->SetLabel("works fine");
	//_**********************************************************************************************
}
//----------------------------------------END guipageki.cpp----------------------------------------
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

*removed*
aalien
In need of some credit
In need of some credit
Posts: 8
Joined: Sat Jan 27, 2007 6:00 pm

Post by aalien »

I found bug, it should be

Code: Select all

this->Connect(ex_Button->GetId(),wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(pageKI::OnButton),0,parent->GetPreviousHandler());
instead

Code: Select all

this->Connect(ex_Button->GetId(),wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(pageKI::OnButton),0,parent->GetParent());
Post Reply