QuickMath TextField Control

Based on another conversation here today, I decided to relieve my boredom by tackling this custom control.
As a result I have a working prototype that duplicates the functionality described in
https://forum.xojo.com/40662-textedit-calculator

it consists of TWO custom controls that work together, and is 100% Xojo code with no declares or platform specific code.
The first control is a subclass of TextField (qmTextField)

  • it accepts ONLY valid numeric entries (+,-, 0-9 and “.”)
  • optionally it can have a qmTAPEFIELD attached, which is activated by pressing the “=” key

The second control is a subclass a listbox (qmTapeField)

  • this emulates a paper tape calculator attached to a qmTextField (it is NOT a standalone control, like qmTextField is)
  • only one instance of this control is required per window and is “shared” by all qmTextFields , and by default it is NOT visible
  • has only ONE property … visibleRows (default and minimum is 4)

qmTextField can stand alone if required and used as a numeric only entry field (in which case “=” is ignored
by adding a line in its open event, you can attach a qmTapeField

me.attachTapeField(qmTapeField1)  // nothing more is required to "related" the two controls

The qmTapeField accepts the following keys

  • 0 to 9, and “.” for numeric entry
    • , - , / , * for mathematical operations (done in the ordered ENTERED, not using equation priority rules)
  • ‘C’ will clear the current entry in progress
  • ‘=’ will display the current answer, and continue without affecting the qmTextField
  • [Esc] will cancel all operations and return the original value (if any) to the qmTextField
  • [Enter], [Return], [Tab] close the qmTapeField and return the current “answer” to the qmTextField
  • qmTapeField automatically positions itself to the location of the qmTextField that called it, and adjusts its own width and height, as well as font

keystrokes involved

  • 3.14159 [entered that value into qmTextField]
  • = [activated the qmTapeField, 2nd picture]
  • / 2 = [ results is the 2nd picture]
  • [ENTER] closed the qmTapeFiled and returned value to the original qmTextField (3rd picture)

But Dave I want the whole pi not a half :). Good work!

LOL… took me a moment to realize what you meant!

A QuickBooks example:

Keystrokes:

  • {tab}
  • 2
  • {enter}
  • {up}
  • .
  • 9
  • {enter}

Neil, remember, Quickbooks integrates Quickmath directly to THEIR forms… so the ability to move within the “spreadsheet” AND alter calculations to another cell is at a level totally beyound a custom TextField control…

what I wrote is a TextField replacement only…

I understand. Specifically I was wondering if you had thought about handling negative numbers and numbers with a percentage sign.

Negative Numbers… yes
% … thought about it… not sure exactly how it should work

270 % 50 = 135?

this morning modified it to allow the dev to limit the input to INTEGER -OR- DOUBLE
and to support “.” or “,” depending on Locale

QB doesn’t allow typing % into the Quick Math, but if the original number was a percentage the answer is returned as a percentage.

QB allows commas to be typed in, but it essentially strips the commas and re-formats. So 1,1234 becomes 11,234.

[quote=331705:@Neil Burkholder]QB doesn’t allow typing % into the Quick Math, but if the original number was a percentage the answer is returned as a percentage.

QB allows commas to be typed in, but it essentially strips the commas and re-formats. So 1,1234 becomes 11,234.[/quote]
commas and decimal points can be problematic since some locales swap the meaning

my control determins which is used as the “decimal point” and allows that to be typed in…
Note : both these controls totally override KEYDOWN and decide what characters are or are not allowed

[quote=331697:@Dave S]Negative Numbers… yes
% … thought about it… not sure exactly how it should work
270 % 50 = 135?
[/quote]
it should be
270 + 50 % = 405
270 - 50 % = 135

[quote=331712:@Jean-Yves Pochez]it should be
270 + 50 % = 405
270 - 50 % = 135[/quote]
no can do… this control is INFIX operators only, (ie X+Y)
it looks at the last result, the current operator, and the current entry
so in you example , it would be waiting for another number

I don’t think you would use % in a calculation. I was simply stating you might not want to disallow it in the finished product (textfield).

which goes back to my question…
How do you see this control handling the “%”?

In my use case it is necessary to allow a % sign for percentage based discounts, although the same field can also be used for a dollar amount. Granted most times I wouldn’t expect the user to use the Quick Math to calculate a percentage but who knows… And it QuickBooks it does work.

My users are using QB everyday and expect my app to work just like QB as it really is designed to become part of QuickBooks.