Search code examples
c++event-handlingwxwidgetswxtextctrl

C++ Event (Focus) Handling


Due to comments I added the following code

(in BasicPanel)

Connect(CTRL_ONE, wxEVT_KILL_FOCUS, (wxObjectEventFunction)&BasicPanel::OnKillFocus);
Connect(CTRL_TWO,wxEVT_KILL_FOCUS, (wxObjectEventFunction)&BasicPanel::OnKillFocus);
Connect(CTRL_THREE, wxEVT_KILL_FOCUS, (wxObjectEventFunction)&BasicPanel::OnKillFocus);
Connect(CTRL_FOUR, wxEVT_KILL_FOCUS, (wxObjectEventFunction)&BasicPanel::OnKillFocus);
Connect(CTRL_FIVE, wxEVT_KILL_FOCUS, (wxObjectEventFunction)&BasicPanel::OnKillFocus);

(enums)

    CTRL_NAME = wxID_HIGHEST + 5, // 6004
    CTRL_ADDRESS = wxID_HIGHEST + 6, // 6005
    CTRL_PHONENUMBER = wxID_HIGHEST + 7, // 6006
    CTRL_SS = wxID_HIGHEST + 8, // 6007
    CTRL_EMPNUMBER = wxID_HIGHEST + 9 // 6008

(The OnKillFocus Function - the declaration is included as suggested)

void BasicPanel::OnKillFocus(wxFocusEvent& event) {
    switch (event.GetId()) {
        case 6004:
            ...
            break;
                ...    ...     ...
    }

All of these added to the code do nothing when the user changes focus on which text box they are using...


Q1:I am using wxWidgets (C++) and have come accost a problem that i can not locate any help. I have created several wxTextCtrl boxes and would like the program to update the simple calculations in them when the user 'kills the focus.' I could not find any documentation on this subject on the wxWidgets webpage and Googling it only brought up wxPython. The two events i have found are: EVT_COMMAND_KILL_FOCUS - EVT_KILL_FOCUS for neither of which I could find any snippet for. Could anyone give me a short example or lead me to a page that would be helpful?

Q2:Would i have to create an event to handle the focus being killed for each of my 8 wxTextCtrl boxes? In the case that i have to create a different event: How would i get each event to differentiate from each other? I know i will have to create new wxID's for each of the wxTextCtrl boxes but how do I get the correct one to be triggered?

class BasicPanel : public wxPanel { ...     
    wxTextCtrl* one;
    wxTextCtrl* two;
    wxTextCtrl* three;
    wxTextCtrl* four; ... }

Solution

  • Okay, first off, here is the code to put in your BasicPanel class:

    void OnKillFocus(wxFocusEvent& event);
    

    Then add the following to the end of your BasicPanel constructor:

    Connect(ID_TEXTCTRL,
            wxEVT_KILL_FOCUS ,
            (wxObjectEventFunction)&BasicPanel::OnKillFocus);
    

    You will need to repeat the above code for each text control and replace ID_TEXTCTRL with the actual ID of the controls.

    Then the code below will get called whenever one of the controls loses focus.

    void BasicPanel::OnKillFocus(wxFocusEvent& event)
    {
        // code goes here...
    }
    

    To determine the ID of the control that generated the event within OnKillFocus, you can use the following:

    event.GetId()