Can I use wxFloatingPointValidator with wxTextEntryDialog?

100 views
Skip to first unread message

Andreas Falkenhahn

unread,
Oct 23, 2021, 1:57:55 PM10/23/21
to wx-u...@googlegroups.com
I'm trying to create a wxTextEntryDialog that only accepts numbers (real and integer). So I was trying to associate a wxFloatingPointValidator with the wxTextEntryDialog like so:

float fval;
wxFloatingPointValidator<float> val(2, &fval, wxNUM_VAL_ZERO_AS_BLANK);
textEntryDialog.SetTextValidator(val);

But this doesn't compile because wxFloatingPointValidator doesn't derive from wxTextValidator so is it not possible to use wxFloatingPointValidator with wxTextEntryDialog? I'm a bit confused because the doc says:

"This validator can be used with wxTextCtrl or wxComboBox (and potentially any other class implementing wxTextEntry interface)"

So I'd guess that wxTextEntryDialog is also based on the wxTextEntry interface so it should allow the use of wxFloatingPointValidator but I don't see how...

--
Best regards,
Andreas Falkenhahn mailto:and...@falkenhahn.com

Vadim Zeitlin

unread,
Oct 23, 2021, 2:02:58 PM10/23/21
to wx-u...@googlegroups.com
On Sat, 23 Oct 2021 19:58:37 +0200 Andreas Falkenhahn wrote:

AF> I'm trying to create a wxTextEntryDialog that only accepts numbers
AF> (real and integer).

If you only needed integer numbers, you could use wxNumberEntryDialog. As
it is, you will have to create your own dialog. Luckily, this is really
pretty simple.

AF> So I was trying to associate a
AF> wxFloatingPointValidator with the wxTextEntryDialog like so:
AF>
AF> float fval;
AF> wxFloatingPointValidator<float> val(2, &fval, wxNUM_VAL_ZERO_AS_BLANK);
AF> textEntryDialog.SetTextValidator(val);
AF>
AF> But this doesn't compile because wxFloatingPointValidator doesn't
AF> derive from wxTextValidator so is it not possible to use
AF> wxFloatingPointValidator with wxTextEntryDialog?

No, it isn't. This is because wxTextEntryDialog API is bad, it should take
any validator and not just wxTextValidator, but not only it has this
method, but also GetTextValidator() which returns wxTextValidator too,
which prevents this from being changed easily.

AF> I'm a bit confused because the doc says:
AF>
AF> "This validator can be used with wxTextCtrl or wxComboBox (and potentially any other class implementing wxTextEntry interface)"
AF>
AF> So I'd guess that wxTextEntryDialog is also based on the wxTextEntry
AF> interface so it should allow the use of wxFloatingPointValidator but I
AF> don't see how...

It can't be done currently. I agree that it should be possible, but it's
not exactly a priority and it's easy to work around this by making your own
dialog, as mentioned in the beginning.

Regards,
VZ

--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/

Andreas Falkenhahn

unread,
Oct 23, 2021, 3:43:45 PM10/23/21
to Vadim Zeitlin
On 23.10.2021 at 20:02 Vadim Zeitlin wrote:

> On Sat, 23 Oct 2021 19:58:37 +0200 Andreas Falkenhahn wrote:

AF>> I'm trying to create a wxTextEntryDialog that only accepts numbers
AF>> (real and integer).

> If you only needed integer numbers, you could use wxNumberEntryDialog.

Hmm, actually, wxNumberEntryDialog allows me to enter any character and
not just numbers on MSW. Is this supposed to behave like that?

Vadim Zeitlin

unread,
Oct 23, 2021, 4:14:48 PM10/23/21
to wx-u...@googlegroups.com
On Sat, 23 Oct 2021 21:44:28 +0200 Andreas Falkenhahn wrote:

AF> On 23.10.2021 at 20:02 Vadim Zeitlin wrote:
AF>
AF> > On Sat, 23 Oct 2021 19:58:37 +0200 Andreas Falkenhahn wrote:
AF>
AF> AF>> I'm trying to create a wxTextEntryDialog that only accepts numbers
AF> AF>> (real and integer).
AF>
AF> > If you only needed integer numbers, you could use wxNumberEntryDialog.
AF>
AF> Hmm, actually, wxNumberEntryDialog allows me to enter any character and
AF> not just numbers on MSW. Is this supposed to behave like that?

Sorry, I forgot that it didn't use any validators. It should, and this
would be a simple enough change without any compatibility implications, so
it really ought to be done. But I can't promise doing it myself, sorry.

Andreas Falkenhahn

unread,
Oct 23, 2021, 5:41:29 PM10/23/21
to Vadim Zeitlin
On 23.10.2021 at 22:14 Vadim Zeitlin wrote:

> Sorry, I forgot that it didn't use any validators.

Now that's a bit funny, though. So there's this class named wxNumberEntryDialog
whose whole purpose is to ensure that only numbers are entered and then it
does everything but not *that* ... who on earth implemented this class and
then decided to not implement the one key feature users expect of this class?
It is not without irony :)

So I'm back at wxTextEntryDialog now using something like this:

wxTextValidator validator(wxFILTER_INCLUDE_CHAR_LIST);
wxArrayString list;
wxString valid_chars("0123456789.-");
size_t len = valid_chars.Length();

for(size_t i = 0; i < len; i++) list.Add(wxString(valid_chars.GetChar(i)));
validator.SetIncludes(list);

textEntryDialog.SetTextValidator(validator);

While this solution doesn't guarantee a correctly formatted number because
- and . can be entered multiple times and in all positions but at least
it blocks all other characters...

Vadim Zeitlin

unread,
Oct 23, 2021, 5:44:49 PM10/23/21
to wx-u...@googlegroups.com
On Sat, 23 Oct 2021 23:42:06 +0200 Andreas Falkenhahn wrote:

AF> On 23.10.2021 at 22:14 Vadim Zeitlin wrote:
AF>
AF> > Sorry, I forgot that it didn't use any validators.
AF>
AF> Now that's a bit funny, though. So there's this class named wxNumberEntryDialog
AF> whose whole purpose is to ensure that only numbers are entered and then it
AF> does everything but not that ... who on earth implemented this class and
AF> then decided to not implement the one key feature users expect of this class?

This class provides the API allowing you to get a number from user. It's
rarely (IME never) the best way to do it, UI-wise, it's only really used in
example code/samples.

AF> So I'm back at wxTextEntryDialog now using something like this:
AF>
AF> wxTextValidator validator(wxFILTER_INCLUDE_CHAR_LIST);

I don't recommend using wxTextValidator, it's behaviour is surprising and
not very user-friendly.

Really, just write your own dialog and use wxFloatingPointValidator. Or,
better yet, add a way to enter this number in your main window, without
opening a modal dialog just for this, if possible.

Regards,
Reply all
Reply to author
Forward
0 new messages