Search code examples
wxwidgets

On Press Enter Key on wxSearchCtrl not firing as expected


I am writing a windows app using wxWidghets on visual studio and plan to go build it for linux when this is up and running well on windows. I have a search control on the main control which I have attached a search on Enter key function on it but it is not doing anything and I cant figure out what I have not done or what else I should do to make it work when the user will press enter on the control.

myframe.h

#pragma once

#include <wx/artprov.h>
#include <wx/xrc/xmlres.h>
#include <wx/string.h>
#include <wx/srchctrl.h>
#include <wx/gdicmn.h>
#include <wx/font.h>
#include <wx/colour.h>
#include <wx/settings.h>
#include <wx/sizer.h>
#include <wx/statbox.h>
#include <wx/frame.h>

class MyFrame : public wxFrame
{
    private:

    protected:
        wxSearchCtrl* TxtSearch;
        void OnSearchButton( wxCommandEvent& event );
        void OnSearchEnter( wxCommandEvent& event );

    public:

        MyFrame( wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = wxEmptyString, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( 500,300 ), long style = wxDEFAULT_FRAME_STYLE|wxTAB_TRAVERSAL );

        ~MyFrame();

};

myframe.cpp

#include "myframe.h"  

MyFrame::MyFrame( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style ) : wxFrame( parent, id, title, pos, size, style )
{
    this->SetSizeHints( wxDefaultSize, wxDefaultSize );

    wxBoxSizer* bSizer1;
    bSizer1 = new wxBoxSizer( wxVERTICAL );

    TxtSearch = new wxSearchCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
    #ifndef __WXMAC__
    TxtSearch->ShowSearchButton( true );
    #endif
    TxtSearch->ShowCancelButton( false );
    bSizer1->Add( TxtSearch, 1, wxALL|wxEXPAND, 5 );

    wxStaticBoxSizer* sbSizer1;
    sbSizer1 = new wxStaticBoxSizer( new wxStaticBox( this, wxID_ANY, wxT("label") ), wxVERTICAL );


    bSizer1->Add( sbSizer1, 1, wxALL|wxEXPAND, 5 );


    this->SetSizer( bSizer1 );
    this->Layout();

    this->Centre( wxBOTH );

    // Connect Events
    TxtSearch->Connect( wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN, wxCommandEventHandler( MyFrame::OnSearchButton ), NULL, this );
    TxtSearch->Connect( wxEVT_COMMAND_TEXT_ENTER, wxCommandEventHandler( MyFrame::OnSearchEnter ), NULL, this );
}

MyFrame::~MyFrame()
{
    // Disconnect Events
    TxtSearch->Disconnect( wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN, wxCommandEventHandler( MyFrame::OnSearchButton ), NULL, this );
    TxtSearch->Disconnect( wxEVT_COMMAND_TEXT_ENTER, wxCommandEventHandler( MyFrame::OnSearchEnter ), NULL, this );

}

MyFrame::OnSearchButton()
{
    SetStatus("You pressed search button");
}

MyFrame::OnSearchEnter()
{
    SetStatus("You pressed enter");
}

the other event for pressing the search button actually works something which I cant fail to understand at the moment. Online when I search I am just seeing more documentation on wxPython and I just cant what speaks for c++. I would appreciate some enlightment on this


Solution

  • @Ripi2's answer is okay but may fail to fire the event as the OP asked. I think you need to look at how you create the wxSearchCtrl and see if you are initiating the search event properly. Change it from:

    TxtSearch = new wxSearchCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
    

    To this:

    TxtSearch = new wxSearchCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER );
    

    Let me know if this works, since I have not tested it myself.