PDA

View Full Version : Delphi help


Nefrit
11-29-2007, 10:07 AM
I decided to test my Delphi knowlege today by making myself a calculator. Took me a while, bu I figured most of it out. It all works well, except for one problem.

If, for example, you press 9 + 2 + 5, what I'd love it to do is to clear the screen when the next button after the plus sign is pressed, entering only the value of that button, clearing the values that were there before it. Can anyone help? I know that it's probably some sort of On..... thing.

Anyone here insane enough to know any Delphi anyway?:confused: :eek: :confused:

Gox
12-01-2007, 08:21 AM
Since you didn't provide your code I'll assume your calculator looks similar to the Windows calculator. I'll also assume that you're using a textbox to show user input.

One (messy) solution is to have a flag that gets set when an operation button is pressed.

Have a global variable boolean clearFlag

Then in your procedure for your operators that gets called when they are clicked,
(I'm not positive about syntax for Sender : Object don't assume I'm correct here)

procedure PlusSignClicked(Sender : Object)
begin
//Do operation and then

clearFlag := true;
end;


Then you'll need to check if this flag is set when your number buttons are clicked


procedure TwobuttonClicked(Sender : Object)
begin
if clearFlag then
begin
textBox.Text := ' '; ///Clear the textbox that shows the entered numbers
clearFlag := false; //Set it back to false
end;

textBox.Text := '2';
...etc..
end;

Unfortunately with this implementation you'd have to add clearFlag := true; to all your operator onClick procedures, and then check this flag and set it back to false if needed in all your number button onClick procedures.

My brain fails to offer a better solution at this hour...