wxwidgets - 在 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++ 说话。我希望能对此有所启发
解决方案
根据官方 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 );
推荐阅读
- javascript - 更改要打印的字体系列和字体大小
- javascript - API 身份验证如何仅使用 javascript 上的公钥?
- delphi - TADODataset 在 Delphi 中捕获 Post 方法错误代码
- maven - Maven 无法找到 AEM 原型
- python - 在使神经元网络适应使用乳腺癌数据库时,形状 (398,398) 和 (1,4) 未对齐错误
- r - 如何计算每个 id 的重复事件
- javascript - 如何使用钩子设置常量“状态”变量?
- r - 使用 dplyr::left_join 和多个条件合并两个数据框
- jquery - 如何使里面的工作可点击
- php - PHP魔术方法__toNumber()将对象转换为数字