Validating textbox input as numeric

We encourage new members to introduce themselves here. Get to know one another and share your interests.
Post Reply
RolWil
Posts: 68
Joined: Mon Jul 18, 2022 3:16 am

Validating textbox input as numeric

Post by RolWil »

Hello, me again. Converting Harbour code to XSharp.

What are you using to validate that a user has entered a number in a textbox? I’ve tried a few things but can’t get it right. I really don’t want to get into workarounds using Regex, checking each character in the string etc. etc. There must be some existing function that does the trick - but I sure can't find it.

My pProject’s References are:
  • XSharp.Core
  • XSharp.RT



    1)The following code sample always results in “Not OK”

    Code: Select all

    testString := SELF:tbTaxRate.Text
    if IsNumeric(testString)
        MessageBox.Show("OK")
    else
        MessageBox.Show("Not OK")
    Endif

    2)Can’t use the following code because Val() always returns zero for an invalid entry … but of course also for zero which IS a valid entry.

    Code: Select all

    testValue := Val(SELF:tbTaxRate.Text)

    Thanks for tips.
User avatar
Chris
Posts: 4665
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Validating textbox input as numeric

Post by Chris »

Hi Roland,

IsNumeric() checks if the type of the param passed is a numeric type (INT, FLOAT etc), so it will indeed always return FALSE, when you pass a STRING to it.

You can use Int32.TryParse() (for integer values only) and Double.TryParse() (for floating point values) instead of Val(), both of those return TRUE if the conversion fron string was successful:

Code: Select all

? Int32.TryParse("abc", OUT VAR dummyInt)
? Double.TryParse("123.456", OUT VAR dummyDouble)
Chris Pyrgas

XSharp Development Team test
chris(at)xsharp.eu
g.bunzel@domonet.de
Posts: 97
Joined: Tue Mar 01, 2016 11:50 am
Location: Germany

Validating textbox input as numeric

Post by g.bunzel@domonet.de »

Roland,

why not set a picture to that textbox. Then there are only numbers possible.

I don't work with Harbour, but something like that should be possible:
SELF:tbTaxRate.Picture := "999.99"

HTH

Gerhard
RolWil
Posts: 68
Joined: Mon Jul 18, 2022 3:16 am

Validating textbox input as numeric

Post by RolWil »

Thanks Chris

In my searches I came across TryParse(), but didn't realize it will return success boolean and not just set the result variable (which will be 0 in the case of non numeric input). Perfect!

Cheers.
RolWil
Posts: 68
Joined: Mon Jul 18, 2022 3:16 am

Validating textbox input as numeric

Post by RolWil »

Thanks Gerhard.

I wish one could do that, but VS doesn't support it, at least not if the dialect is "Harbour"

Cheers.
User avatar
Chris
Posts: 4665
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Validating textbox input as numeric

Post by Chris »

Roland/Gerhard,

The "Picture" way does not work, because this is a member of the GUI classes of VO, not of WinForms. This is one area (data entry/validation), where VO offers far better tools than .Net (out of the box) in my opinion.

It does offer some other related neat features though, Roland, please have a look into the ErrorProvider winforms component, for providing a visual sign to the user of what he has entered wrong etc. Here's also a small sample:

https://learn.microsoft.com/en-us/dotne ... esktop-7.0

.
Chris Pyrgas

XSharp Development Team test
chris(at)xsharp.eu
User avatar
wriedmann
Posts: 3690
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Validating textbox input as numeric

Post by wriedmann »

Hi all,
unfortunately at the current state to support picture masks for Windows Forms or WPF you have to use some 3rd party library, and they are not compatible with the xBase picture masks.
But there should be Willie's RightSLE anywhere, ported to Windows Forms. Maybe we should take it and move it to X#.
Look at http://www.wmconsulting.com/rightSLE.htm
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
RolWil
Posts: 68
Joined: Mon Jul 18, 2022 3:16 am

Validating textbox input as numeric

Post by RolWil »

Thanks again Chris; what I've done for now, is simply validate data entered in the submit-button-event and make a red error label visible next to any 'offending' entry. As I start to convert some of more complex programs that make up my entire package, I'll have to look for a more elegant solution such as employing the 'CausesValidation' property as you suggest.
RolWil
Posts: 68
Joined: Mon Jul 18, 2022 3:16 am

Validating textbox input as numeric

Post by RolWil »

Thank you Wolfgang; for now, I'm using a more primitive - yet functional - approach as you can see in my response to Chris. I tend to shy away from 3rd party products.

Cheers.
User avatar
wriedmann
Posts: 3690
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Validating textbox input as numeric

Post by wriedmann »

Hi Roland,
I agree with you to stay away from 3rd party products whenever possibble, but in this case (RightSLE) we are speaking about an open source implementation of a TextBox that was created by a well known member of the VO and Vulcan community several years ago.
This code is easy to understand, and for sure has no hidden functionalities.
You may also remove code you don't need from this library, like the calculatorSLE or the calendarSLE.
In the long run you will sometimes need to use also 3rd party code if you have the sources.
I have tried to build my WPF applications without 3rd party controls, but I had to admit that that would inhibit me to deliver what my customers requested, and therefore I had to buy a control library (SyncFusion in my case) because I needed an advanced datagrid and an advanced RTF control that can save and read also Word documents.
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Post Reply