Code: Select all
// code generated by DialogBlocks: m_TxtControl is the member variable for the text edit control
m_TxtControl = new wxTextCtrl( itemPanel1, ID_MY_TEXT_CONTROL, wxEmptyString, wxDefaultPosition, wxSize(40, -1), 0 );
// my code: m_fVal is a floating point variable
m_TxtControl->SetValidator(wxFloatingPointValidator<float>(1, &m_fVal, wxNUM_VAL_NO_TRAILING_ZEROES));
static_cast<wxFloatingPointValidator<float> *>(m_TxtControl ->GetValidator())->SetRange(0.1f, 1000.0f);
There are two problems with this, and I wonder if I am doing something wrong or if there is a work-around?
One: If the user wants to enter a valid number like 0.3 and starts by typing "0", the system dings as soon as the "0" is keyed in. If the first character entered is the decimal point ".", then entry can continue. But this is not very friendly - I think most of my users would expect to be able to enter a number less than 1 but greater than 0 by starting with "0.".
Two: if the user does start with "." to avoid the ding, but the enters ".1" it again fails, even though this should be within the allowed range. Looking at the source under debug, I think the problem is that the validator is checking:
Code: Select all
return m_min <= value && value <= m_max;
Even though my code is setting m_min to 0.1, in the debugger it is displaying as something like 0.10000000149. Then when the user keys in ".1", the <= condition fails because of the floating point inaccuracy. In this case the work around is to set the min in code to something like 0.099, but it would be better to tackle this in the source code with some sort of epsilon "slop" in the condition.
Does anyone have any thoughts about this? Is there something simple that I am missing? The failure to allow data entry starting with a 0 is a bit of a deal-breaker in terms of using this validator, at the moment.