1

I have a control system with a lot of tabs. Each of them with many buttons and text controls. I would like to have one single event handler that executes the same code but to different text controls.

For example, 10 text controls have IP values. Instead of 10 events and 10 handlers, i want to use the same event handler function but they need to point to each one's event generation object:

void anyname::OnCheckIP(wxCommandEvent& event)
{
   // code to check IP value for the caller text control
}

¿How can I do that? ¿Is is even possible to recover the caller object pointer inside an event handler?

DarkZeros
  • 8,235
  • 1
  • 26
  • 36

1 Answers1

2

You can subclass the control that you are using, catch the event generated, and call a method to handle the event in the new class. Then you use this new control for all the widgets that need the same handler.

Suppose you have a lot of wxTextCtrls

class cMyCommonTextCtrl : public wxTextCtrl
{
   int myID;
public:
    cMyCommonTextCtrl( int ID, ... )
    : wxTextCtrl( ... ),
    , myID( ID )
    ...
  {
    bind(  wxEVT_TEXT_ENTER, &cMyCommonTextCtrl::OnCheckIP, this, myID );
    ...
  }
    void OnCheckIP( wxCommandEvent& )
    {
        // handle text, using myID attribute to distinguish which one
    }
};
ravenspoint
  • 19,093
  • 6
  • 57
  • 103
  • Thank you, but how can I distinguish each one using the ID? Do I need to do a switch/case? If a getobject(ID) exist it would be great! – DarkZeros Mar 01 '12 at 16:10
  • A switch statement would be the usual way of doing this. You can add a getobject() method if you wish, but if it is only used from within the OnCheckIP it is not necessary. – ravenspoint Mar 01 '12 at 17:10