Reputation:
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
Upvotes: 1
Views: 514
Reputation:
@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.
Upvotes: 1
Reputation: 7198
According to official wxSearchCtrl docs you should handle the wxEVT_SEARCH
event, which is fired both when the search button is clicked and when Enter is pressed in the control.
And also, there's nothing wrong with using Connect()
, but it's better to use Bind()
, see docs.
So putting it all together:
MyFrame::MyFrame(.....)
// 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 );
TxtSearch->Bind( wxEVT_SEARCH, &MyFrame::OnSearchButton, this );
....
//Not required but here it is:
MyFrame::~MyFrame(....)
TxtSearch->Unbind( wxEVT_SEARCH, &MyFrame::OnSearchButton );
Upvotes: 2