Page 1 of 1
Validating textbox input as numeric
Posted: Tue Apr 18, 2023 12:41 am
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.
Validating textbox input as numeric
Posted: Tue Apr 18, 2023 6:26 am
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)
Validating textbox input as numeric
Posted: Tue Apr 18, 2023 7:45 am
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
Validating textbox input as numeric
Posted: Tue Apr 18, 2023 8:30 pm
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.
Validating textbox input as numeric
Posted: Tue Apr 18, 2023 8:32 pm
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.
Validating textbox input as numeric
Posted: Tue Apr 18, 2023 10:34 pm
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
.
Validating textbox input as numeric
Posted: Wed Apr 19, 2023 3:21 am
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
Validating textbox input as numeric
Posted: Wed Apr 19, 2023 4:28 pm
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.
Validating textbox input as numeric
Posted: Wed Apr 19, 2023 4:30 pm
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.
Validating textbox input as numeric
Posted: Thu Apr 20, 2023 4:28 am
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