C# Calculator ~ The final struggle. Bool isn’t working

I've hidden my old post so that it doesn't take up unnecessary space!

I've edited and reformatted my code significantly since my previous post and right now the only problem I'm having is to get bool to work as intended. The idea is that bool starts with a default value of false and when the one of the code's operator's are pressed the value changes to true and clears the occupied text box – which's value has been stored in another variable. I thought I did it correctly but apparently not. I've been staring at this blindly for far too long now, help would be deeply appreciated! This is the code I've come up with.

public partial class Form1 : Form { Calculator kalkylator = new Calculator(); public Form1() { InitializeComponent(); } public void form1_Load(object sender, EventArgs e) { kalkylator.SetFirstOperand = 0; kalkylator.SetSecondOperand = 0; kalkylator.SetOP = ""; kalkylator.SetChange = false; } private void button_Click_Click(object sender, EventArgs e) // Value - Press a number or , on the calculator and the digit appears in the textbox { if (kalkylator.SetChange == true) tbxEntry = null; Button k = (Button)sender; tbxEntry.Text = tbxEntry.Text + k.Text; } private void button19_Click(object sender, EventArgs e) // CE - clears the current entry { tbxEntry.Text = null; } private void button14_Click(object sender, EventArgs e) // Operation - { Button k = (Button)sender; kalkylator.SetOP = k.Text; kalkylator.SetFirstOperand = Double.Parse(tbxEntry.Text); kalkylator.SetChange = true; } private void button18_Click(object sender, EventArgs e) // Equals { switch (kalkylator.SetOP) { case "+": tbxEntry.Text = (kalkylator.SetFirstOperand + Double.Parse(tbxEntry.Text)).ToString(); break; case "-": tbxEntry.Text = (kalkylator.SetFirstOperand - Double.Parse(tbxEntry.Text)).ToString(); break; case "*": tbxEntry.Text = (kalkylator.SetFirstOperand * Double.Parse(tbxEntry.Text)).ToString(); break; case "/": tbxEntry.Text = (kalkylator.SetFirstOperand / Double.Parse(tbxEntry.Text)).ToString(); break; } kalkylator.SetChange = false; } } 

}

by lantsen via /r/csharp

Leave a Reply