Click here to Skip to main content
15,897,291 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear Sir,
I want to add three numbers in textbox and display the result in another textbox using "textchanged event" in c#. if i change any number in any textbox it should reflect in the result textbox. where one textbox vaue is fixed (ie)500.

I tried like this but its not executing.
C#
Int32 ms, p, i, tot;
ms = Convert.ToInt32(txtMonthlySaving.Text);
p = Convert.ToInt32(txtPrincipal.Text);
i = Convert.ToInt32(txtInterest.Text);
tot = ms + p + i;
txtTotal.Text = tot.ToString();



Can anyone clear my doubt.waiting for your result..
Posted
Comments
OriginalGriff 25-Oct-13 12:20pm    
"its not executing" is not very helpful when looking at that code.
What is happening?
Is it compiling and running? Does it ever get into the method? If not, what does it do?
Sergey Alexandrovich Kryukov 25-Oct-13 13:00pm    
In this code you don't even try to handle the event, so what would you expect? :-)
And don't use "Convert", use int.Parse, int.TryParse.
Handle the event, read Text, update other control. All you said is correct. Just do it.
—SA
BillWoodruff 26-Oct-13 1:10am    
Are the three TextBoxes used for input, txtMonthlySaving, txtPrincipal, and txtInterest, "wired-up" to a common TextChanged EventHandler ? Or ?
Ank_ush 26-Oct-13 1:17am    
If you find my solution correct then plz click on accept solution and if any help needed then plz reply.

1 solution

I declared the same int variables as you declared.
C#
int ms, p, i, tot; 

There is no need to write int 32 for declaring variable, if you write int then also it works.

C#
private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                ms = Convert.ToInt32(textBox1.Text);
                tot = ms + p + i;
                textBox4.Text = tot.ToString();
            }
            else
            {
                ms = 0;
                tot = ms + p + i;
                textBox4.Text = tot.ToString();
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            if (textBox2.Text != "")
            {
                p = Convert.ToInt32(textBox2.Text);
                tot = ms + p + i;
                textBox4.Text = tot.ToString();
            }
            else
            {
                p = 0;
                tot = ms + p + i;
                textBox4.Text = tot.ToString();
            }
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            if (textBox3.Text != "")
            {
                i = Convert.ToInt32(textBox3.Text);
                tot = ms + p + i;
                textBox4.Text = tot.ToString();
            }
            else
            {
                i = 0;
                tot = ms + p + i;
                textBox4.Text = tot.ToString();
            }
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900