Implementing wxTimer 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
BobS0327
Knows some wx things
Knows some wx things
Posts: 27
Joined: Wed May 10, 2006 3:51 pm

Implementing wxTimer

Post by BobS0327 »

In the book Cross platform prgramming with wxWidgets there is a wxTimer example that is implemented as follows:

Code: Select all

MyFrame::MyFrame() : m_timer(this, TIMERID)
How would I implement this timer in the following code snippet?

Code: Select all

MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const
                 wxSize&
                 size, long style)
        : wxFrame((wxFrame *)NULL, -1, title, pos, size, style)
{
Thanx in advance for any assistance offered
User avatar
T-Rex
Moderator
Moderator
Posts: 1249
Joined: Sat Oct 23, 2004 9:58 am
Location: Zaporizhzhya, Ukraine
Contact:

Post by T-Rex »

Code: Select all

#include <wx/wx.h>
#include <wx/valgen.h>

class MyApp : public wxApp
{	
public:	
	virtual bool OnInit();	
};

DECLARE_APP(MyApp)
IMPLEMENT_APP(MyApp)

class MyFrame : public wxFrame
{
	wxTimer m_timer;
public:
	MyFrame(const wxString& title, const wxPoint& pos = wxDefaultPosition, 
		const wxSize& size = wxDefaultSize, 
		long style = wxSYSTEM_MENU|wxCAPTION|wxCLOSE_BOX|wxRAISED_BORDER|wxRESIZE_BORDER);
	DECLARE_EVENT_TABLE()
	void OnTimer(wxTimerEvent & event);
};

enum
{
	ID_VALUE_TEXTCTRL = 10001,
	ID_STATUS_TEXTCTRL,
	ID_TEST_TIMER
};

BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_TIMER(ID_TEST_TIMER, MyFrame::OnTimer)
END_EVENT_TABLE()

MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const
		wxSize&	size, long style)
: wxFrame((wxFrame *)NULL, -1, title, pos, size, style), m_timer(this, ID_TEST_TIMER)
{
	m_timer.Start(2000);
	wxBoxSizer * sizer = new wxBoxSizer(wxVERTICAL);
	SetSizer(sizer);
}

void MyFrame::OnTimer(wxTimerEvent & event)
{
	wxMessageBox(_("Timer Test"));
}

bool MyApp::OnInit()
{		
	MyFrame * frame = new MyFrame(_("Timer test"));
	SetTopWindow(frame);
	frame->Centre();
	frame->Show();
	return true;
}
or

Code: Select all

#include <wx/wx.h>
#include <wx/valgen.h>

class MyApp : public wxApp
{	
public:	
	virtual bool OnInit();	
};

DECLARE_APP(MyApp)
IMPLEMENT_APP(MyApp)

class MyFrame : public wxFrame
{
	wxTimer * m_timer;
public:
	MyFrame(const wxString& title, const wxPoint& pos = wxDefaultPosition, 
		const wxSize& size = wxDefaultSize, 
		long style = wxSYSTEM_MENU|wxCAPTION|wxCLOSE_BOX|wxRAISED_BORDER|wxRESIZE_BORDER);
	~MyFrame();
	DECLARE_EVENT_TABLE()
	void OnTimer(wxTimerEvent & event);
};

enum
{
	ID_VALUE_TEXTCTRL = 10001,
	ID_STATUS_TEXTCTRL,
	ID_TEST_TIMER
};

BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_TIMER(ID_TEST_TIMER, MyFrame::OnTimer)
END_EVENT_TABLE()

MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const
		wxSize&	size, long style)
: wxFrame((wxFrame *)NULL, -1, title, pos, size, style)
{
	m_timer = new wxTimer(this, ID_TEST_TIMER);
	m_timer->Start(2000);
	wxBoxSizer * sizer = new wxBoxSizer(wxVERTICAL);
	SetSizer(sizer);
}

MyFrame::~MyFrame()
{
	if(m_timer) wxDELETE(m_timer);
}

void MyFrame::OnTimer(wxTimerEvent & event)
{
	wxMessageBox(_("Timer Test"));
}

bool MyApp::OnInit()
{		
	MyFrame * frame = new MyFrame(_("Timer test"));
	SetTopWindow(frame);
	frame->Centre();
	frame->Show();
	return true;
}
BobS0327
Knows some wx things
Knows some wx things
Posts: 27
Joined: Wed May 10, 2006 3:51 pm

Post by BobS0327 »

THANX!!!!!
vladtess
In need of some credit
In need of some credit
Posts: 5
Joined: Fri Jul 08, 2011 4:36 pm

Re: Implementing wxTimer

Post by vladtess »

Aahh, this was very useful! Thanks!!
ouch67
Earned some good credits
Earned some good credits
Posts: 135
Joined: Sun Mar 23, 2008 12:09 am

Re: Implementing wxTimer

Post by ouch67 »

or even better with dynamic event tables and 2 timers:

Code: Select all

void OnTimer(wxTimerEvent& event)
{
   wxMessageBox("timer 0 says hi");
}

void OnTimers(wxTimerEvent& event)
{
   wxMessageBox("timer 1 says hi");
}

    wxTimer* looptimer[2];

    looptimer[0] = new wxTimer;
    looptimer[1] = new wxTimer;

    looptimer[0]->SetOwner(this,0);
    looptimer[1]->SetOwner(this,1);

    Bind(wxEVT_TIMER, &OnTimer,0);
    Bind(wxEVT_TIMER, &OnTimers,1);

    looptimer[0]->Start(3000);
    looptimer[1]->Start(4500);

Post Reply