How to "capture" a wxTextCtrl changed by the user? Topic is solved

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
wxJack
Experienced Solver
Experienced Solver
Posts: 83
Joined: Wed Jun 20, 2018 8:06 am

How to "capture" a wxTextCtrl changed by the user?

Post by wxJack »

Hi guys,
I'm writing some code where there is a wxTextCtrl, with the style wxTE_PROCESS_ENTER.
I associated the textCtrl to an event with BIND that works when the user changes the tc.
I wrote a function that does the following: when the user changes the wxTextCtrl manually,
a wxString takes the new value inserted by the user.
The function is activated correctly when the event "user changes the tc value and presses enter" happens, but the new value of the tc is an empty string..
How can I solve it?
thank you very much
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: How to "capture" a wxTextCtrl changed by the user?

Post by PB »

You should be always able to obtain the content of wxTextCtrl using its GetValue() method.

In your event handler, you should be able to obtain the wxTextCtrl from which the event came from like this

Code: Select all

#include <wx/wx.h>
#include <wx/textctrl.h>

class MyDialog : public wxDialog
{
public:
    MyDialog() : wxDialog(NULL, wxID_ANY, _("Test"), wxDefaultPosition, wxSize(800, 600))
    {                
        wxTextCtrl* editCtrl = new wxTextCtrl(this, wxID_ANY, "Change me and press <Enter>", 
            wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
        editCtrl->Bind(wxEVT_TEXT_ENTER, &MyDialog::OnTextEnter, this);        
    }
private:
    void OnTextEnter(wxCommandEvent& event)
    {
        wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>(event.GetEventObject());

        if ( textCtrl )
            wxLogMessage("OnTextEnter(): %s", textCtrl->GetValue());    
    }
};

class MyApp : public wxApp
{
public:
    virtual bool OnInit()
    {
        MyDialog().ShowModal();
        return false;
    }
}; wxIMPLEMENT_APP(MyApp);
wxJack
Experienced Solver
Experienced Solver
Posts: 83
Joined: Wed Jun 20, 2018 8:06 am

Re: How to "capture" a wxTextCtrl changed by the user?

Post by wxJack »

oooh thank you very much...I tried using GetLabel() and GetLabelText but these 2 didn't give me the expected value.
I will try with your method! Thanks again!
Post Reply