custom wxTextValidator

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
Nargil
In need of some credit
In need of some credit
Posts: 5
Joined: Mon Jun 16, 2008 2:38 pm

custom wxTextValidator

Post by Nargil »

I want a control that accepts polish postal codes xx-xxx format. So I figured out a custom validator would be best

Code: Select all

class PostalValidator: public wxTextValidator
{
public:
	PostalValidator() :
			wxTextValidator(wxFILTER_INCLUDE_LIST)
	{
		wxString postalchars[11] =
		{ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "-" };
		wxArrayString as(11, postalchars);
		SetIncludes(as);
	}
protected:
	wxObject* Clone() const
	{
		return new PostalValidator();
	}
	bool checkDash(const wxString& str) 
	{
		if(str.Len()>=3 && str[2]!='-')
			return false;
		else return true;
	}
	virtual bool TransferToWindow()
	{
		if (checkDash(*m_stringValue))
			return wxTextValidator::TransferToWindow();
		else
			return false;
	}
	virtual bool TransferFromWindow(void)
	{
		wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow;
		const wxString& tempString = control->GetValue();
		if (checkDash(tempString))
			return wxTextValidator::TransferFromWindow();
		else return false;
	}
};

ContractorPanel::ContractorPanel(bool createNew, wxWindow* parent,
		wxWindowID id, const wxPoint& pos, const wxSize& size, long style) :
		fb_ConrtractorForm(parent, id, pos, size, style)
{
	mCreateNew = createNew;
	m_textPostal->SetValidator(PostalValidator());
}
where fb_ContractorForm is a class i created using wxFormBuilder. The control works just like a standard wxTextValidator, accepting numbers and the dash, but does not execute my code. What did I miss ?
lhksoft
Knows some wx things
Knows some wx things
Posts: 33
Joined: Sat Jan 01, 2022 5:30 pm

Re: custom wxTextValidator

Post by lhksoft »

I thought Clone should be PUBLIC, and you need a copy c'tor

It should more look like something as following :

Code: Select all

class PostalValidator : public wxTextValidator
{
public:
	PostalValidator() : wxTextValidator(wxFILTER_INCLUDE_LIST)
	{
		wxString postalchars[11] =
		{ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "-" };
		wxArrayString as(11, postalchars);
		SetIncludes(as);
	}

	PostalValidator(const PostalValidator& other) : wxTextValidator(other)
	{
		// copy your internal data from 'other'
	}

	virtual wxObject* Clone() const wxOVERRIDE
	{
		return new PostalValidator();
	}

	virtual bool TransferToWindow() wxOVERRIDE
	{
		if ( checkDash(*m_stringValue) )
			return wxTextValidator::TransferToWindow();
		else
			return false;
	}
	virtual bool TransferFromWindow(void) wxOVERRIDE
	{
		wxTextCtrl* control = (wxTextCtrl*)m_validatorWindow;
		const wxString& tempString = control->GetValue();
		if ( checkDash(tempString) )
			return wxTextValidator::TransferFromWindow();
		else return false;
	}

protected:
	bool checkDash(const wxString& str)
	{
		if ( str.Len() >= 3 && str[2] != '-' )
			return false;
		else return true;
	}

private:
	wxDECLARE_DYNAMIC_CLASS_NO_ASSIGN(PostalValidator);
};
and in your implementation-file (.cpp) :

Code: Select all

wxIMPLEMENT_DYNAMIC(PostalValidator, wxTextValidator)

ContractorPanel::ContractorPanel(bool createNew, wxWindow* parent,
								 wxWindowID id, const wxPoint& pos, const wxSize& size, long style) :
	fb_ConrtractorForm(parent, id, pos, size, style)
{
	mCreateNew = createNew;
	m_textPostal->SetValidator(PostalValidator());
}
Be sure that 'm_textPostal' has been created with 'ContractorPanel' as its parent, like :

Code: Select all

m_textPostal = new TextPostalCtrl(ContractorPanel /* its parent */ );
Coding using MS-Code/C++ in linux-mint, and MSVS2019-C++ with vcpkg in Win10. Currently using wxWidgets 3.2.0
Post Reply