TextField Change 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
rafae11
Experienced Solver
Experienced Solver
Posts: 85
Joined: Sat Jun 02, 2012 8:41 am

TextField Change

Post by rafae11 »

i would want to set a variable to either true or false whenever i change a text field.
how do i do this?
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: TextField Change

Post by PB »

You Bind/Connect a wxEVT_COMMAND_TEXT_UPDATED event of wxTextCtrl(s) you're interested in.

By the way, are you aware that there is actually a decent official documentation for wxWidgets and a lot of code samples in wxWidgets's samples directory?
rafae11
Experienced Solver
Experienced Solver
Posts: 85
Joined: Sat Jun 02, 2012 8:41 am

Re: TextField Change

Post by rafae11 »

I am aware thanks samples are a bit complicated for me. I am still learning the basics.
User avatar
xaviou
Super wx Problem Solver
Super wx Problem Solver
Posts: 437
Joined: Mon Aug 21, 2006 3:18 pm
Location: Annecy - France
Contact:

Re: TextField Change

Post by xaviou »

Hi

You probably don't need a variable to do this : just check if your wxTextCtrl has been marked as modified :

Code: Select all

if (the_wxTextCtrl->IsModified())
{
    DoSomethingOrNot();
}
Regards
Xav'
My wxWidgets stuff web page : X@v's wxStuff
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: TextField Change

Post by PB »

While xaviou's example works, if you need to be notified every time the value in the edit control(s) changes, you need to do something like this:

Code: Select all

#include <wx/wx.h>

class MyFrame : public wxFrame
{
public:
    MyFrame()
        : wxFrame(NULL, wxID_ANY, _("Test"))
    {        

        wxBoxSizer* bSizer = new wxBoxSizer(wxVERTICAL);

        wxTextCtrl* textCtrl = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE);
        bSizer->Add(textCtrl, 1, wxEXPAND);
        SetSizer(bSizer);        

       textCtrl->Bind(wxEVT_COMMAND_TEXT_UPDATED, &MyFrame::OnTextUpdated, this);  
                
        m_modified = false;
    }	
private:
    bool m_modified;
    void OnTextUpdated(wxCommandEvent& event)
    {
        // if you need to get wxTextCtrl* that initiated the event, you can do:
        // wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>(event.GetEventObject());                
        if ( !m_modified ) {
            m_modified = true;
            SetTitle(GetTitle().Append(wxS(" *"))); 
           // you need to set m_modified to false and remove the " *" from the title after saving a document etc.
        }
    }
};


class MyApp : public wxApp
{
public:	
	virtual bool OnInit()
	{
		if (!wxApp::OnInit())
			return false;       	

        MyFrame* frame = new MyFrame();
        frame ->Show();

        return true;
	}
};

IMPLEMENT_APP(MyApp)
If you're for some reason still using wxWidgets 2.8, replace line starting with "textCtrl->Bind" with

Code: Select all

textCtrl->Connect(wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler(MyFrame::OnTextUpdated), NULL, this);
rafae11
Experienced Solver
Experienced Solver
Posts: 85
Joined: Sat Jun 02, 2012 8:41 am

Re: TextField Change

Post by rafae11 »

Thank for all the help I greatly appreciate it.
Post Reply