Search code examples
wxwidgets

wxTextValidator fails when used with wxTextEntryDialog


I have a problem that I faced when using wxTextValidator with wxTextEntryDialog, when using wxWidgets 2.9.5

In short, the wxTextValidator seems to have no effect. The user can enter anything into wxTextEntryDialog.

// setup validator
wxString ipAddressFilter[11] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "."}; // authorized characters for IP Address
wxArrayString arraystrIPAddress(11, ipAddressFilter);
wxTextValidator txtvldIPAddress(wxFILTER_INCLUDE_CHAR_LIST); // text validator for IP Address
txtvldIPAddress.SetIncludes(arraystrIPAddress); // sets authorized characters for IP Address

// get ip from user
wxString ip;
auto dialog = new wxTextEntryDialog( this, _("Call button pressed"), _("enter ip"), ip);
dialog->SetValidator(txtvldIPAddress);
dialog->ShowModal();
ip = dialog->GetValue();
wxMessageBox( _("button call pressed."), ip , wxOK|wxICON_INFORMATION, this );

do note that wxTextCtrl works as expected with wxTextValidator:

txtctrlIPAddress = new wxTextCtrl(this, -1, wxEmptyString, wxPoint(0, 5), wxSize(300, 30), wxTE_CENTRE, txtvldIPAddress);

Solution

  • You are using a wrong method. SetValidator() sets the validator associated with the dialog itself, which is never used. You need SetTextValidator() instead, which associates the validator with the text control inside the dialog.