Just getting into C# and programming in general, i'm making a calculator with the windows form app project template. i'm trying to make my calculator work like a regular one does where if you press 1+1 and then + again it shows the sum of the 1+1 and waits for the next number input to add to that.
My code for hitting any of the number buttons is tb1.Text = tb1.Text + "the number being pressed"
I have defined 3 variables so far int num1 = 0; int num2 = 0; int result;
my code for the onclick of the "+" button is as follows
private void button11_Click(object sender, EventArgs e)
{
//Add
if (num1 == 0)
{
num1 = Convert.ToInt32(tb1.Text);
tb1.Clear();
}
else
{
num2 = Convert.ToInt32(tb1.Text);
result = num1 + num2;
tb1.Text = Convert.ToString(result);
num1 = result;
tb1.Clear();
tb2.Text = Convert.ToString(num1);
tb3.Text = Convert.ToString(num2);
}
}
this code works and i can see it working because i have debugging text boxes(tb2 and tb3) and tb2 shows
me that num1 is = to the sum of the numbers i am adding together. i am able to add two numbers and then
add another number to that, so on. but id like the sum of the first two numbers to be displayed in tb1
when i hit the + sign again to add another number to it. i figured i would do that by adding
tb1.Text = Convert.ToString(num1);
before my if statement (right below the //Add comment) in the addition button click method. this instead makes nothing show up in the
debug tb2 textbox and doesn't fix my problem and im unsure why. even if i parse it back to an int right
after this new line nothing changes. if more or clearer information is needed, i will oblige
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…