Textfield as currency - type letter crashes

I’ve got some textfields defined and formatted as Currency.

Using it here
if val(IncomeAmt(ii).text) <>0 then
tvalue = IncomeAmt(ii).text
cvalue = Currency.FromString(tvalue,locale) + cvalue
end if
When I type a letter key into it crashes. How can I get around this?

Maybe you want to check that the field contents are numeric before trying to convert to currency.

Edit: this may help: https://documentation.xojo.com/api/language/is.htmlNumeric

This helped in getting rid of letters vs numbers, but now I can’t type in a decimal point. It ignores it if I type it in in sequence as the next thing after numbers. If I go back and type it between two numbers it works. Working with Currency in XOJO seems to be a nightmare.

If you can share a sample project we can help you better.

How do you define the textfields to be formatted as Currency? Is this a Desktop project?

If a Desktop project maybe you can use the Validation Mask for the TextField and add 999999.99 to only allow numbers, the decimal point and 2 digits for cents.

Here’s my code for TextChange for IncomeAmt(index).text

This is in the Format box in the IDE for IncomeAmt(index).text $##,###,###.00

If I type a sequence of numbers followed by a decimal point, it ignores it.

My Objectives: 1. Don’t crash when I type in a letter.
2. Be able to type in decimal numbers

var value, taxx, gpr, cvalue, c as currency
var tvalue, gs, ms, cr as String
Var locale As New Locale("en-US")
Var b As Boolean

b = IsNumeric(IncomeAmt(index).text) //CHECKS IF NUMERIC VALUE
If b Then  //TRUE
  tvalue = IncomeAmt(index).text
  c = Currency.FromString(tvalue,locale) 
  IncomeAmt(index).text = str( c)
  c= val(IncomeAmt(index).text)
  IncomeAmt(index).text = str(c )
end if

Your decimal point is “ignored” because your code removes it. Specially, this line:

IncomeAmt(index).text = str( c)

The TextChange event is called for each character you type. So as soon as you type the decimal point your code re-assigns a value to the field without the decimal point.

Re-assigning a value to the field is perhaps not the best idea here, but it’s not clear what you are actually trying to accomplish.

If you are just trying to get the typed value into a Currency variable, then you probably want something like this:

Var c As Currency
Var value As String = IncomeAmt(index).Text
  c = Currency.FromString(value, Locale.Current)
  Label1.Text = c.ToString(Locale.Current)
Catch e As RuntimeException
  // The text that was typed is not a valid currency
  Label1.Text = value
End Try

Keep in mind that this will drop into the debugger anytime an invalid currency value is entered, but you can click Resume to allow the Catch code to run. More about exception handling is here:


I’m just trying to type into IncomeAmt(index) so I can add up 9 items in the IncomeAmt(index) control set without it dropping into the debugger if I accidentally type in a letter.

I don’t need the result to be a Label.text. I need the simple result to be IncomeAmt(index) as Currency with decimal points.

In that case, here’s one option. Get rid of the Format you have on the IncomeAmt field and try this code in the TextChange event:

Const kControlSetMax = 8 // You want 8 here since there are 9 fields
Var sum As Currency
For i As Integer = 0 To kControlSetMax
  Var value As String = IncomeAmt(i).Text
  If IsNumeric(value) Then
    sum = sum + Currency.FromString(value, Locale.Current)
  End If

Label1.Text = sum.ToString(Locale.Current)

Any time something is typed in one of the fields, it loops through every field and checks if its value is numeric. If it is numeric then it includes it in the sum, which is then displayed in a Label.