[英]Manually trigger an ENTER key press event on wxTextCtrl in wxWidgets for C++?
I have this text control:我有这个文本控件:
FTextCtrl = new wxTextCtrl(parent, wxID_ANY, _("Text"), wxPoint(20, 20), wxDefaultSize, wxTE_CENTRE | wxTE_PROCESS_ENTER, wxDefaultValidator, _T("ID_TC"));
FTextCtrl->Connect(wxEVT_CHAR, (wxObjectEventFunction)OnTextCharEvnt, NULL, this);
This function OnTextCharEvnt
gets called whenever an Enter key is pressed on the text control.每当在文本控件上按下Enter键时,都会调用此 function
OnTextCharEvnt
。
How can I manually trigger this event (when my text cursor is inside the text control) and call this function OnTextCharEvnt
using the same?如何手动触发此事件(当我的文本 cursor 在文本控件内时)并使用相同的方法调用此 function
OnTextCharEvnt
?
I use EVT_TEXT_ENTER
It works;我使用
EVT_TEXT_ENTER
它有效;
sample.h样本.h
enum{
TEXT_FTextCtrl
}
sample.cpp样本.cpp
FTextCtrl = new wxTextCtrl(parent, TEXT_FTextCtrl, wxT("Text"), wxPoint(20, 20), wxDefaultSize, wxTE_CENTRE | wxTE_PROCESS_ENTER, wxDefaultValidator, _T("ID_TC"));
BEGIN_EVENT_TABLE(Sample, wxFrame)
EVT_TEXT_ENTER(TEXT_FTextCtrl, Sample::OnTextCharEvnt)
END_EVENT_TABLE()
You don't need to trigger any events to call your own function -- just call it directly instead.您无需触发任何事件即可调用您自己的 function —— 直接调用即可。 Calling the event handler directly is awkward and not recommended, but this is a trivial solution avoiding it -- just have a normal function that you can call both from that event handler and directly:
直接调用事件处理程序很尴尬,不推荐,但这是一个避免它的简单解决方案 - 只需有一个普通的 function ,您可以从该事件处理程序和直接调用:
void Sample::OnTextCharEvnt(wxCommandEvent& event) { DoHandleCharEvent(event.GetUnicodeKey()); }
void Sample::DoHandleCharEvent(wchar_t key) { ... your actual code for handling it ... }
void Sample::SomeOtherFunction() { DoHandleEnter(); }
If you used the recommended Bind()
function rather than old Connect()
, you could even avoid having the trivial OnTextCharEvnt()
entirely by using a lambda instead, eg:如果您使用推荐的
Bind()
function 而不是旧的Connect()
,您甚至可以通过使用 lambda 来完全避免使用琐碎的OnTextCharEvnt()
,例如:
FTextCtrl->Bind(wxEVT_CHAR, [](wxCommandEvent& event) {
DoHandleCharEvent(event.GetUnicodeKey());
});
All this ignores the fact that you should not be using wxEVT_CHAR
with wxTextCtrl
in the first place, but should handle either wxEVT_TEXT
or wxEVT_TEXT_ENTER
or both of them, instead of lower level char events (that won't get triggered at all if the user pastes something in the control, for example).所有这些都忽略了这样一个事实,即您首先不应该将
wxEVT_CHAR
与wxTextCtrl
一起使用,而应该处理wxEVT_TEXT
或wxEVT_TEXT_ENTER
或两者,而不是较低级别的字符事件(如果用户粘贴,则根本不会触发例如,控件中的某些东西)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.