Hi,
I am creating an instance of wxTextCtrl. However, depending on the type (text, integer or decimal), the wxTextCtrl instance should display a message when user enters a wrong value. For example, if the type is text, and the user enters a numeric value, a message should be displayed saying the value is invalid.
For this, I am associating a wxValidator. Please see the code below:
if (attrParams.type == "1")
{
wxTextValidator val(wxFILTER_ALPHA, &(textCtrl->m_strValue));
textCtrl->SetValidator(val);
}
else if (attrParams.type == "2")
{
wxIntegerValidator<int> val(&(textCtrl->m_intValue));
textCtrl->SetValidator(val);
}
else if (attrParams.type == "3")
{
wxFloatingPointValidator<float> val(&(textCtrl->m_decimalValue));
textCtrl->SetValidator(val);
}
The Validator is working fine, as in it doesnt allow the user to enter invalid values. But if I add the code below(in order to display a message to user) after each "SetValidator" call, it crashes in case of wxTextValidator. It does not crash when used with wxIntegerValidator and wxFloatingPointValidator, but the message box is not getting displayed either.
if (val.Validate(textCtrl->GetParent()) == false) { wxMessageBox("invalid value"); }
Please see the callstack below:
Please help!
wxValidator : Display error message when validation fails
Re: wxValidator : Display error message when validation fails
Hi,
First of all - please use the <code></code> tags so that the code you post will be readable.
Second - thank you for the backtrace, but it is not useful - we have absolutely no idea what those functions are and how the functions are communi=cating with each other.
Third - you should clarify whether you are getting the crash or the program just asserts.
And last but not least, usual stanza - wx version, OS/toolkit version.
Thank you.
First of all - please use the <code></code> tags so that the code you post will be readable.
Second - thank you for the backtrace, but it is not useful - we have absolutely no idea what those functions are and how the functions are communi=cating with each other.
Third - you should clarify whether you are getting the crash or the program just asserts.
And last but not least, usual stanza - wx version, OS/toolkit version.
Thank you.
Re: wxValidator : Display error message when validation fails
This looks a bit suspicious, where did you get your val from? I do not think that you are supposed to validate like this at all. You set a validator to the control and the control does the validation as needed.deepti wrote:Code: Select all
if (val.Validate(textCtrl->GetParent()) == false) { wxMessageBox("invalid value"); }
If you want to change how it validation works you should override the validator methods of the control or when enough just call
Code: Select all
if ( !textCtrl->Validate() ) ...
Re: wxValidator : Display error message when validation fails
Hi PB,
I found that from the documentation for wxValidator in this site :
http://docs.wxwidgets.org/trunk/classwx ... 436e5c71af
Using the if (!textCtrl->Validate() ) is not helping either. I need a message box to be displayed when the datatype of the value entered is incorrect.
Right now, it just does not let the user enter incorrect values. But that is confusing since the user might not know why numbers are not getting entered while text is!
Is there a way to get this done? Please help
I found that from the documentation for wxValidator in this site :
http://docs.wxwidgets.org/trunk/classwx ... 436e5c71af
Using the if (!textCtrl->Validate() ) is not helping either. I need a message box to be displayed when the datatype of the value entered is incorrect.
Right now, it just does not let the user enter incorrect values. But that is confusing since the user might not know why numbers are not getting entered while text is!
Is there a way to get this done? Please help
Re: wxValidator : Display error message when validation fails
I do not see any such code there.deepti wrote:Hi PB,
I found that from the documentation for wxValidator in this site :
http://docs.wxwidgets.org/trunk/classwx ... 436e5c71af
I do not think it is a good idea and AFAIK this not how it is usually done. It would be very frustrating for the user to be slapped by a message box every time he tries to type an "invalid" character. This is usually handled by a system beep, which can unfortunately not be heard by the user. In any way, as I told you before, you probably need to use a custom validator derived from a wxWidgets one, with its Validate() overriden.deepti wrote: Using the if (!textCtrl->Validate() ) is not helping either. I need a message box to be displayed when the datatype of the value entered is incorrect.
Right now, it just does not let the user enter incorrect values. But that is confusing since the user might not know why numbers are not getting entered while text is!
Is there a way to get this done? Please help
Re: wxValidator : Display error message when validation fails
Hi PB,
There is this function at the end of the page in the link I sent:
There is this function at the end of the page in the link I sent:
Code: Select all
virtual bool wxValidator::Validate(wxWindow * parent)
virtual
This overridable function is called when the value in the associated window must be validated.
Parameters
parent The parent of the window associated with the validator.
Returns
false if the value in the window is not valid; you may pop up an error dialog.
Reimplemented in wxTextValidator, and wxNumericPropertyValidator.
Re: wxValidator : Display error message when validation fails
The code there has nothing in common with that code of yours I quoted? I.e., it does not have a local instance of wxValidator calling validate with an unassociated control.deepti wrote:Hi PB, There is this function at the end of the page in the link I sent: