0

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.

How can I manually trigger this event (when my text cursor is inside the text control) and call this function OnTextCharEvnt using the same?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Himanshu Poddar
  • 7,112
  • 10
  • 47
  • 93
  • Why not simply call `OnTextCharEvnt` directly? Or better, move its code to a separate function that both `OnTextCharEvnt` and You can call when needed. – Remy Lebeau Jun 09 '20 at 07:11
  • Yeah ok! but how do I call this function with EVENT wxEVTCHAR. How do I call this function explicitly with that event – Himanshu Poddar Jun 09 '20 at 07:15
  • you are missing the point. You don't need to simulate input, you can just call your own function directly when you need it. – Remy Lebeau Jun 09 '20 at 07:17
  • But calling this function directly `OnTextCharEvnt()` gives me error, like one argument required – Himanshu Poddar Jun 09 '20 at 07:32
  • 1
    obviously, since events have arguments to describe the events. But if you write a separate function, you don't have to give it any arguments if you don't need to, eg: `void OnTextCharEvnt(wxKeyEvent& event) { if (event.GetKeyCode() == WXK_RETURN) doSomething(); }` where `doSomething()` is the new function that you can call anywhere else you need it. – Remy Lebeau Jun 09 '20 at 09:08
  • But if this is too above your current skill level, then have a look at the text control's [`EmulateKeyPress()`](https://docs.wxwidgets.org/3.0/classwx_text_ctrl.html#aac3e7f2124aafd67df6f6efbc285a40b) method instead. – Remy Lebeau Jun 09 '20 at 09:09
  • I got it what you said and I after some research I did using the same way – Himanshu Poddar Jun 09 '20 at 09:56

2 Answers2

0

I use EVT_TEXT_ENTER It works ;

sample.h

enum{
 TEXT_FTextCtrl
}

sample.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()
Donot Don't
  • 609
  • 7
  • 12
0

You don't need to trigger any events to call your own function -- just call it directly instead. 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:

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, e.g.:

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

VZ.
  • 21,740
  • 3
  • 39
  • 42