disable dialog wxButton if any changes to wxTextCtrl 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
bajra
In need of some credit
In need of some credit
Posts: 9
Joined: Sun Jun 22, 2008 9:01 am

disable dialog wxButton if any changes to wxTextCtrl

Post by bajra »

hi,
i recently wrote a dialog that has a couple of wxTextCtrl.The OK button is initially disabled.I would like to know how i can enable it after user has entered atleast one character into the wxTextCtrl.I am trying to achieve this by using

Code: Select all

EVT_UPDATE_UI(idOkButton,myFrame::OnTextCtrlChange)
than later on the OnTextCtrlChange function

Code: Select all

void myFrame::onTextCtrlChange(wxUpdateUIEvent& event)
{
     wxTextCtrl* myT=(wxTextCtrl*) FindWindow(idTitleCtrl);
    if(myT->GetValue().empty())
    {
        event.Enable(true);
    }
    else
    {
        event.Enable(false);
    }
}
but it doesnt seem to work.Please help.
benedicte
wxWorld Domination!
wxWorld Domination!
Posts: 1409
Joined: Wed Jan 19, 2005 3:44 pm
Location: Paris, France

Post by benedicte »

Do not use EVT_UPDATE_UI but EVT_TEXT or EVT_TEXT_ENTER.

A wxUpdateUiEvent is generated on a menu/toolbar item to let you check/uncheck or enable/disable it...
You need to handle the text input event to enable the button.
bajra
In need of some credit
In need of some credit
Posts: 9
Joined: Sun Jun 22, 2008 9:01 am

Post by bajra »

but EVT_TEXT accepts wxCommandEvent as an argument.But wxCommandEvent doesnt have member function Enable so i wont be able to enable or disable the button.

doing

Code: Select all

event.Enable(true)
wont work with EVT_TEXT.

i am new to wx so it would be very helpful if you could provide some code samples.
eranif
Moderator
Moderator
Posts: 610
Joined: Tue Nov 29, 2005 7:10 pm
Location: Israel

Post by eranif »

Do it like this:

Code: Select all

void MyFrame::OnText(wxCommandEvent &event)
{
 wxTextCtrl* myT=(wxTextCtrl*) FindWindow(idTitleCtrl);
 // find the button, or maybe just keep it as member of the
 // frame?
 wxButton* myB=(wxButton*) FindWindow(idbutton);
 if(myT && myB){ 
   mtY->GetValue().IsEmpty() ? myB->Enable(false) : myB->Enable();
  }
}

Btw, I would keep the controls as members rather then finding them using FindWindow().

Eran

IDE: CodeLite + wxCrafter
OS: All
https://wxcrafter.codelite.org
https://codelite.org
bajra
In need of some credit
In need of some credit
Posts: 9
Joined: Sun Jun 22, 2008 9:01 am

Post by bajra »

yes i am using them as members.i did what you said but it isn't working.am i doing something wrong here:

implementation file:

Code: Select all

BEGIN_EVENT_TABLE(myFrame,wxFrame)
    ....
    EVT_TEXT(idTextCtrl,myFrame::OnChange)
    ....
END_EVENT_TABLE()
then later at function definition

Code: Select all

void OnChange(wxCommandEvent& event)
{
    //ctrl and button are members so i access them directly
    myTextCtrl->GetValue().IsEmpty() ? myButton->Enable(false) : myButton->Enable(true);

}

but this isn't working.any thoughts ?
eranif
Moderator
Moderator
Posts: 610
Joined: Tue Nov 29, 2005 7:10 pm
Location: Israel

Post by eranif »

Does the event handler get called?

Can u place a wxMessageBox(wxT("Inside handler")) at that function and confirm that it works?

I suspect it does not get call.

Try to connect the event like this:
after the creation of the text control, call this:

Code: Select all

myTextCtrl->Connect( wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler( myFrame::OnText ), NULL, this );
Eran
IDE: CodeLite + wxCrafter
OS: All
https://wxcrafter.codelite.org
https://codelite.org
bajra
In need of some credit
In need of some credit
Posts: 9
Joined: Sun Jun 22, 2008 9:01 am

Post by bajra »

yes that solved the problem.thanks :D

one question though.I dont see connect method inside wxTextCtrl object.So how come myTextCtrl->Connect(..) is possible ? and why didnt the static event handler worked ?
eranif
Moderator
Moderator
Posts: 610
Joined: Tue Nov 29, 2005 7:10 pm
Location: Israel

Post by eranif »

Connect() is member of wxEvtHandler which wxtextCtrl derives from.

The Connect() method allows you to connect events from one control and handle them inside different control using the eventSink.

http://docs.wxwidgets.org/stable/wx_wxe ... lerconnect

Eran
IDE: CodeLite + wxCrafter
OS: All
https://wxcrafter.codelite.org
https://codelite.org
bajra
In need of some credit
In need of some credit
Posts: 9
Joined: Sun Jun 22, 2008 9:01 am

Post by bajra »

ahh thanks :o
Post Reply