Oracle Forms计算器退格按钮实现代码咨询
Hey there, let's get that backspace button working for your Oracle Forms calculator. I've tackled this exact scenario before, so here's a solid, easy-to-implement solution:
1. Create the Trigger
First, open up your Forms Builder and locate your backspace button. Right-click the button, select Triggers, then create a new WHEN-BUTTON-PRESSED trigger. This is where we'll add the logic to remove the last character from your calculator's display item.
2. Core Backspace Logic
Paste this PL/SQL code into the trigger, making sure to replace BLOCK_NAME and DISPLAY_ITEM with the actual names of your data block and the item that shows the calculator's current value:
DECLARE v_current_display VARCHAR2(100); -- Adjust length based on your display's max input BEGIN -- Grab the current value from the display item v_current_display := :BLOCK_NAME.DISPLAY_ITEM; -- Only backspace if there's something to remove IF LENGTH(v_current_display) > 0 THEN -- Trim off the last character :BLOCK_NAME.DISPLAY_ITEM := SUBSTR(v_current_display, 1, LENGTH(v_current_display) - 1); END IF; END;
3. Optional Enhancements
Depending on your calculator's functionality, you might want to add these tweaks:
Handle Decimal Points Gracefully
If you want to make sure deleting a decimal point works as expected (instead of just removing any last character), use this adjusted code:
DECLARE v_current_display VARCHAR2(100); BEGIN v_current_display := :BLOCK_NAME.DISPLAY_ITEM; IF LENGTH(v_current_display) > 0 THEN -- If last character is a decimal, remove it IF SUBSTR(v_current_display, -1, 1) = '.' THEN :BLOCK_NAME.DISPLAY_ITEM := SUBSTR(v_current_display, 1, LENGTH(v_current_display) - 1); ELSE -- Otherwise remove the last digit/character :BLOCK_NAME.DISPLAY_ITEM := SUBSTR(v_current_display, 1, LENGTH(v_current_display) - 1); END IF; END IF; END;
Avoid Leaving a Lone Minus Sign
If your calculator supports negative numbers, add a check to clear the display instead of leaving just a - when backspacing a negative number with one digit:
DECLARE v_current_display VARCHAR2(100); BEGIN v_current_display := :BLOCK_NAME.DISPLAY_ITEM; IF LENGTH(v_current_display) = 1 AND v_current_display = '-' THEN -- Clear the display if only the minus sign remains :BLOCK_NAME.DISPLAY_ITEM := ''; ELSIF LENGTH(v_current_display) > 0 THEN :BLOCK_NAME.DISPLAY_ITEM := SUBSTR(v_current_display, 1, LENGTH(v_current_display) - 1); END IF; END;
That's it! Test the button out, and it should remove the last character from your calculator's display every time you click it.
内容的提问来源于stack exchange,提问作者Ramiz Tariq

