How to "capture" a wxTextCtrl changed by the user?  [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.
wxJack
Experienced Solver
Experienced Solver
Posts: 69
Joined: Wed Jun 20, 2018 8:06 am

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

Postby wxJack » Sun Sep 16, 2018 10:26 am

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: 1734
Joined: Sun Jan 03, 2010 5:45 pm

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

Postby PB » Sun Sep 16, 2018 10:47 am

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: 69
Joined: Wed Jun 20, 2018 8:06 am

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

Postby wxJack » Sun Sep 16, 2018 11:21 am

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!


Return to “C++ Development”

Who is online

Users browsing this forum: No registered users and 21 guests