'My BackSpace button and Operation buttons are not in harmony and are interrupting 2 different functions
I am making a calcutor app, I will provide the code below: Here is the problem, when i am performing an addition calculation, lets say (3+3+4 = 10) now when i press the backspace key, it works fine and it gets reset to (6 +) and then i can write another number, lets say (5), so it becomes (6 + 5) now and the answer is (11).
However, when after pressing (=), eg- (3+3+4 = 10), if i press (+) once again, instead of making the answer go (10 +), it goes (16 +).
I think it happens because when pressing (+) again, it thinks the 'operationPerformed' is still (+).
In order to tackle that, I added a new line which resets the 'operationPerfomed' after a calculation, this fixes the latter problem but fucks up the prior one and now I cannot perform backspace after hitting (=) :(
Can somebody help me with this, I cannot figure it out cuz I am very new to this (only been coding for 4 weeks, followed a tutorial for this app)
Here is the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Calculator_App_2
{
public partial class Form1 : Form
{
double initialValue = 0;
string operationPerformed = "";
bool isoperationperformed = false;
public Form1()
{
InitializeComponent();
AnswerBox.Enabled = false;
}
private void btnCharacter_click(object sender, EventArgs e)
{
AnswerBox.Enabled = true;
if(AnswerBox.Text == "0")
{
AnswerBox.Clear();
}
else
{
// Do Nothing.
}
Button button = (Button)sender;
if(button.Text == ".")
{
if(AnswerBox.Text.Contains("."))
{
// Do Nothing.
}
else
{
AnswerBox.Text = AnswerBox.Text + button.Text;
}
}
else
{
AnswerBox.Text = AnswerBox.Text + button.Text;
}
}
private void btnIDK_Click(object sender, EventArgs e)
{
AnswerBox.Clear();
AnswerBox.Text = "Even IDK what's happening ";
lblPreview.Text = " LOL BRUH";
}
private void btnOperation_click(object sender, EventArgs e)
{
if (initialValue != 0)
{
btnEquals.PerformClick();
}
Button button = (Button)sender;
operationPerformed = button.Text;
initialValue = double.Parse(AnswerBox.Text);
lblPreview.Text = initialValue.ToString() + " " + operationPerformed;
AnswerBox.Clear();
}
private void btnEquals_Click(object sender, EventArgs e)
{
if(operationPerformed == "+")
{
isoperationperformed = true;
lblPreview.Text = lblPreview.Text + " " + double.Parse(AnswerBox.Text).ToString();
AnswerBox.Text = (initialValue + double.Parse(AnswerBox.Text)).ToString();
operationPerformed = "";
}
else if(operationPerformed == "-")
{
isoperationperformed = true;
lblPreview.Text = lblPreview.Text + " " + double.Parse(AnswerBox.Text).ToString();
AnswerBox.Text = (initialValue - double.Parse(AnswerBox.Text)).ToString();
operationPerformed = "";
}
else if (operationPerformed == "×")
{
isoperationperformed = true;
lblPreview.Text = lblPreview.Text + " " + double.Parse(AnswerBox.Text).ToString();
AnswerBox.Text = (initialValue * double.Parse(AnswerBox.Text)).ToString();
operationPerformed = "";
}
else if (operationPerformed == "÷")
{
isoperationperformed = true;
lblPreview.Text = lblPreview.Text + " " +double.Parse(AnswerBox.Text).ToString();
AnswerBox.Text = (initialValue / double.Parse(AnswerBox.Text)).ToString();
operationPerformed = "";
}
else if (operationPerformed == "%")
{
isoperationperformed = true;
lblPreview.Text = lblPreview.Text + " " + double.Parse(AnswerBox.Text).ToString();
AnswerBox.Text = ((initialValue / 100) * double.Parse(AnswerBox.Text)).ToString();
operationPerformed = "";
}
}
private void btnAC_Click(object sender, EventArgs e)
{
AnswerBox.Clear();
AnswerBox.Text = "0";
initialValue = 0;
lblPreview.Text = "Calculator";
AnswerBox.Enabled = false;
}
private void btnBackspace_Click(object sender, EventArgs e)
{
if (isoperationperformed == true && AnswerBox.Text.Length > 0)
{
lblPreview.Text = initialValue.ToString() + " " + operationPerformed;
AnswerBox.Text = double.Parse(AnswerBox.Text).ToString();
AnswerBox.Text = AnswerBox.Text.Remove(AnswerBox.Text.Length - 1);
}
else if(AnswerBox.Text.Length > 0)
{
AnswerBox.Text = AnswerBox.Text.Remove(AnswerBox.Text.Length - 1);
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
