User forums > General (but related to Code::Blocks)

Using Timer in wxWidgets with wxSmith

(1/1)

Tomi:
Hello!

I'm using C::B 20.03 and wxWidgets 3.0.x. on Windows 10 (64 bit).
I would like use correctly a timer in my C++ program with wxSmith, but I don't know, how can I add an own function to a created timer at runtime?
I tried so:


--- Code: ---wxw30testappFrame::wxw30testappFrame(wxWindow* parent,wxWindowID id)
{
    //(*Initialize(wxw30testappFrame)
    Create(parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("wxID_ANY"));

    Connect(wxEVT_PAINT,(wxObjectEventFunction)&wxw30testappFrame::OnPaint);
    Connect(wxEVT_KEY_DOWN,(wxObjectEventFunction)&wxw30testappFrame::OnKeyDown);
    //*)
    void ScrRefresh(wxTimerEvent& event);
    wxTimer* scrrefresher=new wxTimer();
    scrrefresher->Start(25);
    //scrrefresher->Bind(wxEVT_TIMER,ScrRefresh,this)
    Connect(wxEVT_TIMER,(wxObjectEventFunction)&wxw30testappFrame::ScrRefresh);
}
(...)
void wxw30testappFrame::ScrRefresh(wxTimerEvent& event)
{
    Update();
}
--- End code ---

But I get the following error messages after these:
error: 'ScrRefresh' is not a member of 'wxw30testappFrame'
error: no declaration matches 'void wxw30testappFrame::ScrRefresh(wxTimerEvent&)'

Somebody can explain me that what is the correct way to assign a function to a Timer event?

Miguel Gimenez:
You cannot declare a method inside another method, it should be declared in the class declaration, probably in the protected section.

You are using wxSmith, so let it create the method and Connect() to it. Just add the wxTimer to your frame, select it, click on the curly brackets in the property grid and add an event handler.

Tomi:

--- Quote from: Miguel Gimenez on January 23, 2024, 04:25:16 pm ---You are using wxSmith, so let it create the method and Connect() to it. Just add the wxTimer to your frame, select it, click on the curly brackets in the property grid and add an event handler.

--- End quote ---
Hello Miguel!

I tried this solution and it finally works now; thanks! But what if I would like create a Timer at runtime? Where is its place and how can I declare it? I didn't find anything about it on the Internet.

Miguel Gimenez:
Just copy what wxSmith did, but outside the //(* ... //*) block: declare a method, define the method and Connect() or Bind() to it.

Navigation

[0] Message Index

Go to full version