0

The function of the form is that if I put a number in the text box and click the add button, the label shows the number. Then, if I put another number in the text box and click the add button, the label shows the sum of those two numbers.

Example: If I put 10 in the text box and click the add button, the label shows 10. Then I put 8 in the text box and click the add button; the label shows 10 + 8 = 18. Then I put 2 in the text box and click the add button; the label shows 18 + 2 = 20.

I created the form application, but it is not working correctly. Can anyone please give me a solution to this problem?

namespace ICT_Project_Prac
{
    public partial class Form1 : Form
    {
        private int sum;
        public Form1()
        {
            InitializeComponent();
            sum = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                sum = int.Parse(textBox1.Text);
                sum = +sum;
                label1.Text = sum.ToString();
            }
            catch
            {
                MessageBox.Show("Please Ender The Valid Number");
            }
        }
    }
}
4
  • 3
    Use your debugger. Commented Jun 4, 2024 at 16:29
  • 3
    Go through each line and think about what is happening. Then you will find the error(s) very easily. Commented Jun 4, 2024 at 16:32
  • 2
    You may find this useful: How to Debug Small Programs In your code, identify which specific operation isn't doing what you expect it to do. What are the values used in that operation? What is the observed result? What was the expected result? Why? Commented Jun 4, 2024 at 16:39
  • 1
    sum = +sum; won't do anything because e.g. +5 = 5. Use int n = int.Parse(textBox1.Text); sum += n; Commented Jun 4, 2024 at 16:49

2 Answers 2

1

Your problem is that you allways put "sum = int.Parse(textBox1.Text);" so you loose your "sum" value before you sum with the new value.

You should do this.

namespace ICT_Project_Prac
{
    public partial class Form1 : Form
    {
        private int sum;
        public Form1()
        {
            InitializeComponent();

            sum = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                int textBoxValue = int.Parse(textBox1.Text);
                sum += textBoxValue;
                label1.Text = sum.ToString();
            }
            catch
            {
                MessageBox.Show("Please Ender The Valid Number");
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

first thing you are adding new value to the sum wrong if you want to add a value to the current value of a variable you can use on of these methods lets say you want to add 2 to sum:

sum = sum + 2;// method 1
sum += 2;     // method 2

as for the way you should read the value inside of the text box and add it to the sum:

    private int sum = 0;
    public Form1()
    {
        InitializeComponent();            
    }

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            int currentValue = int.Parse(textBox1.Text);
            label1.Text = $"{sum} + {currentValue} = {sum + currentValue}";
            sum += currentValue;

        }
        catch (Exception)
        {
            MessageBox.Show("Please Ender The Valid Number");
        }

    }

here is what's wrong with your code:

            sum = int.Parse(textBox1.Text); // here you are changing the value of sum to the value in the text box.

            sum = +sum; // here you are changing the value of sum to be positive sum (+sum).

            label1.Text = sum.ToString(); // this way will change the text of the label to be the sum value only which is not the way you want it like 5 + 3 = 8.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.