M
M
Masalix2021-01-31 23:24:13
C++ / C#
Masalix, 2021-01-31 23:24:13

How to make the textbox show the whole c# calculation example in the calculator?

Hello!
I'm writing a calculator on c# windows forms and ran into a problem that when I enter the first number and click on the plus, the first number disappears. I need to show all the calculations in the textbox, i.e. 1 + 2

The code on the "+" button

n = 1;
c1 = Convert.ToDouble(TextBox1.Text);
TextBox1.Text = "";


Code on button "="

if (n == 1)
{
c2 = Convert.ToDouble(TextBox1.Text);
c3 = c1 + c2;
TextBox1.Text =Convert.ToString (c3);
}

A simple solution is preferred. Thank you in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Zagaevsky, 2021-02-01
@zagayevskiy

Write the text in the text field, and parse the expression into a separate structure (AST or polis). Count by pressing =. Don't mix calculation logic and UI output.

D
DeOxygen, 2021-02-02
@DeOxygen

You must not assign a value to the TextBox, but add ie.
You have TextBox1.Text="" and should TextBox1.Text+="+"
TextBox1.Text =Convert.ToString (c3); and should TextBox1.Text+= "="+Convert.ToString (c3)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question