Custom OnExit event problem 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
User avatar
Ambush
Knows some wx things
Knows some wx things
Posts: 27
Joined: Mon Jun 12, 2017 6:08 pm

Custom OnExit event problem

Post by Ambush »

Hi all,
i am struggling to dynamically create the event and attach it to my button,
i have tried quite a few different methods but cant seem to get them to work
with my beginner code and was hoping someone can show me what i am doing wrong.

Header

Code: Select all

#include <wx\wx.h>

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

DECLARE_APP(Amazing);
Cpp

Code: Select all

#include "Amazing.h"

IMPLEMENT_APP(Amazing);

bool Amazing::OnInit()
{
	wxFrame *frame = new wxFrame(NULL, wxID_ANY, wxString("Title"), wxPoint(wxDefaultPosition), wxSize(0, 0),  wxBORDER_NONE);

	wxBoxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);

	wxButton *btnexit = new wxButton(frame, wxID_EXIT, "Exit App");

	btnexit->Bind(wxEVT_MENU, wxCommandEvent& event,  frame->Close(true), wxID_EXIT);
        
   sizer->AddStretchSpacer();
	sizer->Add(btnexit);
	sizer->AddStretchSpacer();

	frame->SetSizerAndFit(sizer);
	
	frame->Center();
	frame->Show();

	return true;
	
};
Is the way i am coding the app ok as well ?

Thanks.
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Custom OnExit event problem

Post by doublemax »

Code: Select all

btnexit->Bind(wxEVT_MENU, wxCommandEvent& event,  frame->Close(true), wxID_EXIT);
If you really wanted to use a Lambda function here, it should look like this:

Code: Select all

btnexit->Bind( wxEVT_BUTTON, [=](wxCommandEvent &event) {
  frame->Close();
}, wxID_EXIT );
Use the source, Luke!
User avatar
Ambush
Knows some wx things
Knows some wx things
Posts: 27
Joined: Mon Jun 12, 2017 6:08 pm

Re: Custom OnExit event problem

Post by Ambush »

Hey there and thanks !

to be honest, i dont know what a lambda is,
i have been trying to get anything i could find to work
and this seemed like the only thing that was going to.

do you know of another another way that is better for a beginner to use ?
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Custom OnExit event problem

Post by doublemax »

In most samples you find the event handler will be a class method.

Like this:

Code: Select all

#include <wx\wx.h>

class Amazing : public wxApp
{
  public :
    virtual bool OnInit();
    void OnButton( wxCommandEvent &evt );

    wxFrame *m_frame;
};

IMPLEMENT_APP(Amazing)

bool Amazing::OnInit()
{
   m_frame = new wxFrame(NULL, wxID_ANY, wxString("Title"), wxPoint(wxDefaultPosition), wxSize(0, 0),  wxBORDER_NONE);

   wxBoxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);

   wxButton *btnexit = new wxButton(m_frame, wxID_EXIT, "Exit App");
   btnexit->Bind( wxEVT_BUTTON, &Amazing::OnButton, this );

   sizer->AddStretchSpacer();
   sizer->Add(btnexit);
   sizer->AddStretchSpacer();

   m_frame->SetSizerAndFit(sizer);
   
   m_frame->Center();
   m_frame->Show();

   return true;
   
};

void Amazing::OnButton( wxCommandEvent &evt )
{
  m_frame->Close();
}
This is just the equivalent to your version. Usually you would derive your own class from wxFrame and the event handler would be a method of that class.

Please study the "minimal" sample that comes with wxWidgets and the following tutorial to learn the basic "anatomy" of a wxWidgets application:
http://zetcode.com/gui/wxwidgets/
Use the source, Luke!
User avatar
Ambush
Knows some wx things
Knows some wx things
Posts: 27
Joined: Mon Jun 12, 2017 6:08 pm

Re: Custom OnExit event problem

Post by Ambush »

thankyou doublemax

this explains a lot, as i had tried to get the zetcode and other examples modified to work
but still dont fully grasp the header, cpp relationship, even after reading about it numerous times.
Post Reply