首页 > 解决方案 > 在 wxSearchCtrl 上按 Enter 键未按预期触发

问题描述

我正在windows使用 Visual Studio 编写一个应用程序wxWidghets,并计划在 Windows 上启动并运行良好时为 linux 构建它。我在主控件上有一个搜索控件,我在其上附加了一个对 Enter 键功能的搜索,但它没有做任何事情,我无法弄清楚我没有做什么或者我应该做些什么来使它在用户时工作将在控件上按 Enter。

我的框架.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();

};

我的框架.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");
}

按下搜索按钮的另一个事件实际上是我目前无法理解的。当我在网上搜索时,我只是看到了更多关于 wxPython 的文档,而我只是无法为 c++ 说话。我希望能对此有所启发

标签: wxwidgets

解决方案


根据官方 wxSearchCtrl 文档,您应该处理该wxEVT_SEARCH事件,该事件在单击搜索按钮和在控件中按下 Enter 时触发。

而且,使用没有什么问题Connect(),但使用更好Bind(),请参阅文档

所以把它们放在一起:

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 );

推荐阅读