How do you set an event function from inside a class?

For example:

void OnNameChanged(const nana::arg_textbox& arg)
{
   //
}

class DbForm : public nana::form
{
public:
   DbForm() : nana::form{}, name{this->handle()}
   {
      // the following does not work, NameChanged is a member function
      name.events().text_changed(NameChanged);
      // the following works, OnNameChanged is a stand-alone function
      name.events().text_changed(OnNameChanged);
   }
   ~DbForm();

private:
   nana::textbox name;

   void NameChanged(const nana::arg_textbox& arg);
};

So how do I set it to NameChanged?

How do you set an event function from inside a class? For example: ```` void OnNameChanged(const nana::arg_textbox& arg) { // } class DbForm : public nana::form { public: DbForm() : nana::form{}, name{this->handle()} { // the following does not work, NameChanged is a member function name.events().text_changed(NameChanged); // the following works, OnNameChanged is a stand-alone function name.events().text_changed(OnNameChanged); } ~DbForm(); private: nana::textbox name; void NameChanged(const nana::arg_textbox& arg); }; ```` So how do I set it to NameChanged?

So how do I set it to NameChanged?

You can't, unless you make it a static member function. A better solution is to use a lambda, which you can do in two ways:

  1. directly use the lambda:
    name.events().text_changed([this](const nana::arg_textbox &arg)
    {
       std::cout << "lambda text_changed event handler" << std::endl;
    });
    
  2. wrap the lambda in a std::function object:
    std::function<void(const nana::arg_textbox&)> NameChanged = [this](const nana::arg_textbox &arg)
    {
       std::cout << "NameChanged" << std::endl;
    };
    name.events().text_changed(NameChanged); // in the constructor
    
>So how do I set it to NameChanged? You can't, unless you make it a `static` member function. A better solution is to use a lambda, which you can do in two ways: 1. directly use the lambda: ```` name.events().text_changed([this](const nana::arg_textbox &arg) { std::cout << "lambda text_changed event handler" << std::endl; }); ```` 2. wrap the lambda in a `std::function` object: ```` std::function<void(const nana::arg_textbox&)> NameChanged = [this](const nana::arg_textbox &arg) { std::cout << "NameChanged" << std::endl; }; name.events().text_changed(NameChanged); // in the constructor ````
edited Aug 20 at 12:34 pm

You can call private member functions of a class from event handlers in the class in a natural way by using a lambda function that captures this.

Here is the code for a complete working application.


#include <iostream>
#include <nana/gui.hpp>
#include <nana/gui/widgets/textbox.hpp>


class DbForm : public nana::form
{
public:
    DbForm() : nana::form{}, name{this->handle()}
    {
        name.move( { 20,20,100,30 } );

        name.events().text_changed([ this ]( const nana::arg_textbox& arg )
        {
            NameChanged( arg );
        });

    }

private:
    nana::textbox name;

    void NameChanged(const nana::arg_textbox& arg)
    {
        nana::msgbox m("Name Changed member function called");
        m.show();
    }

};

int main()
{
    DbForm f;
    f.show();
    nana::exec();
}
You can call private member functions of a class from event handlers in the class in a natural way by using a lambda function that captures _this_. Here is the code for a complete working application. ```` #include <iostream> #include <nana/gui.hpp> #include <nana/gui/widgets/textbox.hpp> class DbForm : public nana::form { public: DbForm() : nana::form{}, name{this->handle()} { name.move( { 20,20,100,30 } ); name.events().text_changed([ this ]( const nana::arg_textbox& arg ) { NameChanged( arg ); }); } private: nana::textbox name; void NameChanged(const nana::arg_textbox& arg) { nana::msgbox m("Name Changed member function called"); m.show(); } }; int main() { DbForm f; f.show(); nana::exec(); } ````

Thanks all. I guess, I just have to make a call to the member function then, from inside a lambda.

Thanks all. I guess, I just have to make a call to the member function then, from inside a lambda.

I just have to make a call to the member function then, from inside a lambda.

Yes. But do not forget to capture this

> I just have to make a call to the member function then, from inside a lambda. Yes. But do not forget to capture _this_
edited Aug 20 at 6:05 pm
29
views
4
replies
3
followers
live preview
enter atleast 10 characters
WARNING: You mentioned %MENTIONS%, but they cannot see this message and will not be notified
Saving...
Saved
All posts under this topic will be deleted ?
Pending draft ... Click to resume editing
Discard draft