wxTextCtrl EVT_TEXT_ENTER

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
p_lu
Knows some wx things
Knows some wx things
Posts: 26
Joined: Thu Oct 25, 2018 2:04 pm

wxTextCtrl EVT_TEXT_ENTER

Post by p_lu » Mon Feb 11, 2019 9:11 pm

I'm trying to use a wxTextCtrl widget with EVT_TEXT_ENTER mode (wxTE_PROCESS_ENTER style set under wxsmith in Codeblocks).

Codeblocks, by default, sets up a handler, OnTextCtrl1Text(...) which gets invoked on _any_ change of text in the widget. I want the handler invoked when the ENTER key is pressed (or invoked when any key OR the ENTER key is pressed, having the handler only perform some action on the ENTER). I modified the event in the .wxs file to use the EVT_TEXT_ENTER event rather than the EVT_TEXT event but the handler is still only being invoked on a normal key/text change, and _not_ on an ENTER key pressed. Of course, the idea is to process the text string only when the ENTER key is pressed.

I think I read the wxTextCtrl reference properly, but perhaps I'm misinterpreting how this stuff works.

Any help highly appreciated. Thanks.

User avatar
doublemax
Moderator
Moderator
Posts: 13297
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxTextCtrl EVT_TEXT_ENTER

Post by doublemax » Mon Feb 11, 2019 10:44 pm

Platform, wxWidgets version?

Is this a single or multiline wxTextCtrl?

Can you show the code CB generates? This should include construction of the control, binding the event handler and the event handler thenselves.
Use the source, Luke!

medusa1414
In need of some credit
In need of some credit
Posts: 5
Joined: Wed Mar 18, 2015 3:19 pm

Re: wxTextCtrl EVT_TEXT_ENTER

Post by medusa1414 » Mon Feb 11, 2019 11:31 pm

I wrote this fully functional 1 file program example for you to show example usage of the "run a function when enter is pressed in the textbox"-function.
Running Windows 7, wxWidgets 3.1.2.

Code: Select all

#include "wx/wxprec.h"

#ifndef WX_PRECOMP
#   include "wx/wx.h"
#endif

#include <wx/artprov.h>
#include <wx/xrc/xmlres.h>
#include <wx/string.h>
#include <wx/textctrl.h>
#include <wx/gdicmn.h>
#include <wx/font.h>
#include <wx/colour.h>
#include <wx/settings.h>
#include <wx/sizer.h>
#include <wx/frame.h>

class MyFrame1 : public wxFrame
{
    private:
        DECLARE_EVENT_TABLE()

	protected:
		wxTextCtrl* m_textCtrl1;
		wxTextCtrl* m_textCtrl2;
        void TestFunction1(wxCommandEvent& event);
        void TestFunction2(wxCommandEvent& event);

	public:
		MyFrame1( wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = wxEmptyString, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( 447,369 ), long style = wxDEFAULT_FRAME_STYLE|wxTAB_TRAVERSAL );
		~MyFrame1();

};


enum {
    IDTEXT1, IDTEXT2
};


class HelloWorldApp : public wxApp
{
public:
    virtual bool OnInit();
};


DECLARE_APP(HelloWorldApp)
IMPLEMENT_APP(HelloWorldApp)


BEGIN_EVENT_TABLE(MyFrame1, wxFrame)
    EVT_TEXT_ENTER(IDTEXT1, MyFrame1::TestFunction1)
    EVT_TEXT_ENTER(IDTEXT2, MyFrame1::TestFunction2)
END_EVENT_TABLE()


bool HelloWorldApp::OnInit()
{
    MyFrame1* frame = new MyFrame1(
                        0L, wxID_ANY, wxT("Enter Event Test"), 
                        wxDefaultPosition, wxSize(961, 715),
                        wxCLOSE_BOX | wxDEFAULT_FRAME_STYLE | 
                        wxMAXIMIZE_BOX | wxMINIMIZE_BOX | wxTAB_TRAVERSAL);

    frame->Show();
    return true;
}



/**
 * You pressed enter in the first box!
 */
void MyFrame1::TestFunction1(wxCommandEvent& event)
{
    wxMessageBox(wxT("You pressed enter in the first box!"), wxT("Hello"));
}

/**
 * You pressed enter in the multiline textbox!
 */
void MyFrame1::TestFunction2(wxCommandEvent& event)
{
    wxMessageBox(wxT("You pressed enter in the multiline textbox!"), wxT("Hello"));
}




MyFrame1::MyFrame1( 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 );

	m_textCtrl1 = new wxTextCtrl( this, IDTEXT1, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER );
	bSizer1->Add( m_textCtrl1, 0, wxALL, 5 );

	m_textCtrl2 = new wxTextCtrl( this, IDTEXT2, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_PROCESS_ENTER  );
	bSizer1->Add( m_textCtrl2, 0, wxALL, 5 );


	this->SetSizer( bSizer1 );
	this->Layout();

	this->Centre( wxBOTH );
}

MyFrame1::~MyFrame1()
{
}

User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2112
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania
Contact:

Re: wxTextCtrl EVT_TEXT_ENTER

Post by evstevemd » Tue Feb 12, 2019 4:20 am

textctrl1 should respond to Enter event as you want. However, tectctrl2 is a multiline control so enter will add a new line instead
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
[Ubuntu 15.04/Windows 10 Pro - GCC/MinGW, CodeLite IDE et al]

medusa1414
In need of some credit
In need of some credit
Posts: 5
Joined: Wed Mar 18, 2015 3:19 pm

Re: wxTextCtrl EVT_TEXT_ENTER

Post by medusa1414 » Tue Feb 12, 2019 2:59 pm

Both of them respons to enter in my case. Multiline box doesnt add a new line.

I read this on the wiki now:
"Multiline wxTextCtrls do not support wxTE_PROCESS_ENTER, and should never get EVT_TEXT_ENTER events. The fact that this sometimes works on MSW is actually a (minor) bug. "

p_lu
Knows some wx things
Knows some wx things
Posts: 26
Joined: Thu Oct 25, 2018 2:04 pm

Re: wxTextCtrl EVT_TEXT_ENTER

Post by p_lu » Tue Feb 12, 2019 6:56 pm

I tried out your MyFrame1 program and it works great as expected.

However, since I'm using the Codeblocks+wxsmith infrastructure to build my project, I tried a standalone test of the wxTextCtrl widget and am still seeing the handler invoked on _any_ change to the text line. By eyeballing your (working) and my (failing) code, I'm so far not able to figure out what the critical difference is. I see wxTE_PROCESS_ENTER passed to the wxTextCtrl constructor in either case. Below, I've attached the code generated by wxsmith. Since your code works, I can probably keep digging and figure out the difference by brute force.

I'm using Codeblocks17 with VS2017 on Windows10 (wxWidgets 3.0.4).

Thanks for all help.

===
textctrlMain.h
(1.09 KiB) Downloaded 3 times
textctrlMain.cpp
(2.25 KiB) Downloaded 5 times

p_lu
Knows some wx things
Knows some wx things
Posts: 26
Joined: Thu Oct 25, 2018 2:04 pm

Re: wxTextCtrl EVT_TEXT_ENTER

Post by p_lu » Tue Feb 12, 2019 7:13 pm

Found the problem.

In the Connect(), I should use wxEVT_COMMAND_TEXT_ENTER instead of the default wxEVT_COMMAND_TEXT_UPDATED. This should correspond to your use of EVT_TEXT_ENTER(). Not sure how to tell wxsmith to do this, though, at this point, but at least I know what's needed.

Thank you so much.

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 3159
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxTextCtrl EVT_TEXT_ENTER

Post by ONEEYEMAN » Tue Feb 12, 2019 7:38 pm

Hi,
This is the reason "not to use" any RAD tool. ;-)
When you start with wxWidgets, you should get the concept of the sizers and how to work with them.
You should also understand how to do event handling - what events are propagated and what are not, how to pass events to the system, dynamic vs static event handling, etc.
You should also have a good grip on C++ to understand how to do coding in this language.

Thank you.

p_lu
Knows some wx things
Knows some wx things
Posts: 26
Joined: Thu Oct 25, 2018 2:04 pm

Re: wxTextCtrl EVT_TEXT_ENTER

Post by p_lu » Thu Feb 14, 2019 4:34 pm

Yah, it would be nice if we had the luxury of learning basic toolkits from scratch.

But, the reality is that we have very limited time to get software working, so have to count on "add-on" builders like Codeblocks/wxsmith to get an infrastructure going quickly, and to (reverse) learn the basics through the constructed stuff.

In the end, when stuff doesn't work, it's really nice that there are forums like this with experts who can help and who can confirm that feature x indeed works in way w on version v of some software package.

I was working with Windows Named Pipe in C++ recently and it's amazing how convoluted that facility is, with poor documentation, few examples, and no easy way to find (human) help. Had to re-work my code about a half dozen times, over several months, until it actually did what I needed it to do. Even then, I'm not 100% clear "why" it works. But, just looking at the mess tells one how poorly engineered the API is. For comparison, the C# equivalent works "just right" with far fewer lines of interfacing user code. Main point is, I couldn't find a forum to get help.

Thanks.

Post Reply