skip to Main Content

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");
            }
        }
    }
}

2

Answers


  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");
                }
            }
        }
    }
    
    Login or Signup to reply.
  2. 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.
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search