I’m creating a simple calculator on VB6.
Here’s my code I’m working on:
textScreen.Text = textScreen.Text & "+"
Here’s the result when I press some number buttons, followed by clicking on
the plus sign button several times:
75+++++++
I would like the plus sign to appear only once, even if I click on
its button many times:
92+
…and when I click on some number buttons again, followed by clicking
on the plus sign button, I would like the plus sign to show up again:
58+4+
This is somehow similar to the default Calculator on Windows 7.
Well, there are different approaches for this. But in general, I wouldn’t just concatenate some string. This way you’ll have to parse the string later on, instead of just solving the requested term. Instead try to create some stack with your operations/numbers on it; just look on the web for calculator examples.
Anyway, for this, you’ll have to somehow store the last operation (e.g. did I input a digit or an operator?)
If you’d like to limit the calculator to simple operations without brackets etc. you can use a boolean value for this:
Then, before adding the
+(or any other operator):When adding any digit (e.g.):
(Don’t count on 100% error free code, I think I haven’t touched VB6 for like 8 years.)