使用textchanged事件添加数字 [英] add numbers using textchanged event

查看:178
本文介绍了使用textchanged事件添加数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的先生,

我想在文本框中添加三个数字,并在c#中使用textchanged event将结果显示在另一个文本框中。如果我更改任何文本框中的任何数字,它应该反映在结果文本框中。其中一个文本框vaue被修复(即)500。



我试过这样但它没有执行。

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.

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..

推荐答案

我声明了与您声明的相同的int变量。
I declared the same int variables as you declared.
int ms, p, i, tot; 



没有必要写int 32来声明变量,如果你写int然后它也可以工作。




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

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


这篇关于使用textchanged事件添加数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆