I am doing an application which has some calculation code. I would like to know if there is possible to remember the number from the previous event handler?

Lets say that I put into textbox1 number 10 and into textBox2 number 5.
When the even button1_Pressed starts the code has to sum these two number up.


In case if I press the same button again and I did not change any number in those two textBoxes, the code MUST NOT sum those numbers again.

How to achive this? It has to remember the values from the previous even somehow, that I can check the values in textBoxes from before and now.
And if there is no changes do not do the calculation, but if the numbers are different (just one of them, pr both) do the calculation.

I recommend you to use array...you store the current values then use conditional statements in checking the values...

If you want values to persist outside of the event handler, then you need to store them in variables that exist outside the scope of the event handler. Any variables you declare inside the event handler code block are lost when the block exits.
You can create variables at the class level and store the values in these variables each time you sum them:

public partial class Form1 : Form
    {
        //class level variables persist for as long as the instance of the class
        private string lastValue1 = "";
        private string lastValue2 = "";

        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            //if one of the values has changed
            if (textBox1.Text != lastValue1 || textBox2.Text != lastValue2)
            {
                //do your sum process here

                //update stored values
                lastValue1 = textBox1.Text;
                lastValue2 = textBox2.Text;
            }
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.