Tuesday, June 14, 2011

ssDropCalc - a dropdown calculator class

Update November 7, 2011:

Changed the buttons from native VFP to my own design.  Now this looks better in appearance.  These buttons also changes color on mouse enter event.

Added the ability to choose between normal appearance and semi-transparent via _ltransparent property (Default value is .F.).  Just turn it into .T. via PEM window and you will have something like this:

Removed also some properties that I don't need anymore like nThemes, nColor, etc.


Release 2 (June 16, 2011):


a. Added switching capability between showing formula (Ony Too's suggestion) or values on the dropdown calculator class. Formulas will be enclosed with the necessary parenthesis. Default is to show formula instead of values. It can be toggled by clicking on the labels F (Formula) or V (Values) on the face of ssdropcalc.

b. Fixed problem on positioning (done applying Ony Too's approach on his popup calendar using WinAPI, many thanks!). This also fixed problem when parent form is top level.

c. Added formula error detection (as mentioned by Tushar). When an error is detected, a message "Formula Error" will appear on the upper right corner of the formula/value box and the class will attempt to fix it for you by either removing the faulty entry or infusing the proper operator before your value.

d. Backspace will smartly detect and remove extra open and close parenthesis. So you can change your formula anytime except when the dropdown is deactivated (area outside the dropdown calculator is clicked or you have pressed spacebar) where the result will be transferred to the textbox class. From the value there, you can start a new formula or computation.

e. Clicking on an operator will smartly detect previous operators and replace those if necessary

f. Added Copy to Clipboard button. This is not that much necessary but if you want to transfer your formula to excel for quick evaluation of the result, you can use this. This will work even if dropdown is set to show values.

g. Removed SET CLASSLIB hardcoding inside the class (as mentioned by Yousfi). This actually should not be there in the final distribution

Release 1 (Jun 14, 2011):

Last Friday, alternating between checking payroll documents and waiting for more to be submitted to me, I decided to post advances given to staff on my advances module. This advances is based on a single invoice related to several employees so I have to manually compute each individual share (a simple average calculation of Net Purchases divided by number of staff for that invoice). And for that I use Excel if it is already open or windows calculator.

Then a thought hit me. Why not create a simple dropdown calculator for numeric textboxes so I don't need to invoke anymore those outside apps if and when a manual calculation is needed? And when there is no payroll document yet (as they submit to me one at a time payroll documents of a specific project for cross-checking and approval), I started and continued working on that idea. The basic things are set but it still lack some more features so I continued playing with it this weekend.

Finally, I think this is ready to be shared. And because I love having the ability to somewhat change the appearance of my classes, here are the things it can do aside from of course basic mathematical calculations:

a. The ability to set number of decimals. On class init:
This._nDecimal = 4 (default is 2)

b. The ability to remove Themes. On class init:
This._lTheme = .F. (default is .T.)

c. The ability to change calculator background color. On class init:
this._color = rgb(255,255,255)

d. The ability to remove shadow. On class init:
This._lShadow = .F. (default is .T.)

I thought of applying _settings() style again but then decided against it on this particular class.

Very important!!! When you place something on Class Init event, after any or all of the above properties, put:

Some more things:

You can either click on calculator icon or press spacebar when the numeric textbox is in focus to show the dropdown calculator. Pressing spacebar again while dropdown is shown will hide it (similar to combobox style = 2 feature).

When dropdown is shown, you can either click on buttons or use your keyboard for calculation. For numbers and operators, just press the corresponding keys for those. Additional keys are:

Esc = clear Button
Backspace = BS button
= key = Equals button
Spacebar = Hides dropdown calculator (you can do this also via clicking outside of the calculator area using mouse)
R = Memory Recall (MR Button)
C = Clear Memory (MC Button)
P = Memory Plus (M+ Button)
M = Memory Minus (M- Button)

You can continue with the current value meaning after a result has been transferred to the numeric textbox and you already have hidden the dropdown calculator, you can invoke it again and continue calculation from that number. If you want to start anew, just press Esc or hit the Clear button.

Getting its value is just similar to an ordinary textbox:

lnValue = thisform.ssdropcal1.value

You can use this class in place of native textboxes with numeric values to give more "cool effect" to your apps. And like all of my ssclasses (except with images), this is done in 100% native vfp objects and codes fashion.