两个文本框,textchange促发事件,当两个文本框中都为数字时两者相乘放到第三个文本框,求代码....

解决方案 »

  1.   

    前台<div>
            <asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged" AutoPostBack="true "></asp:TextBox>
            *
            <asp:TextBox ID="TextBox2" runat="server" OnTextChanged="TextBox2_TextChanged" AutoPostBack="true "></asp:TextBox>
            =
            <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        </div>cs页面namespace WebApplication2
    {
        public partial class test1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {        }
            int num1 = 0;
            int num2 = 0;
            protected void TextBox1_TextChanged(object sender, EventArgs e)
            {
                if (!string.IsNullOrEmpty(TextBox1.Text.Trim()))
                {
                    num1 = ConvertStringToInteger(TextBox1.Text.Trim());
                }
                if (!string.IsNullOrEmpty(TextBox2.Text.Trim()))
                {
                    num2 = ConvertStringToInteger(TextBox2.Text.Trim());
                }
                TextBox3.Text = (num1 * num2).ToString();
            }        protected void TextBox2_TextChanged(object sender, EventArgs e)
            {
                if (!string.IsNullOrEmpty(TextBox1.Text.Trim()))
                {
                    num1 = ConvertStringToInteger(TextBox1.Text.Trim());
                }
                if (!string.IsNullOrEmpty(TextBox2.Text.Trim()))
                {
                    num2 = ConvertStringToInteger(TextBox2.Text.Trim());
                }
                TextBox3.Text = (num1 * num2).ToString();
            }        private static int ConvertStringToInteger(string s)
            {
                int result = 0;
                int.TryParse(s, out result);
                return result;
            }
        }
    }
      

  2.   


    <div>
            <asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged" AutoPostBack="true "></asp:TextBox>
            *
            <asp:TextBox ID="TextBox2" runat="server" OnTextChanged="TextBox2_TextChanged" AutoPostBack="true "></asp:TextBox>
            =
            <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        </div>namespace WebApplication2
    {
        public partial class test1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {        }
            int num1 = 0;
            int num2 = 0;
            protected void TextBox1_TextChanged(object sender, EventArgs e)
            {            num1 = ConvertStringToInteger(TextBox1.Text.Trim());
                num2 = ConvertStringToInteger(TextBox2.Text.Trim());
                TextBox3.Text = (num1 * num2).ToString();
            }        protected void TextBox2_TextChanged(object sender, EventArgs e)
            {
                num1 = ConvertStringToInteger(TextBox1.Text.Trim());
                num2 = ConvertStringToInteger(TextBox2.Text.Trim());
                TextBox3.Text = (num1 * num2).ToString();
            }        private static int ConvertStringToInteger(string s)
            {
                int result = 0;
                int.TryParse(s, out result);
                return result;
            }
        }
    }
      

  3.   

    在窗体中摆三个TextBox,分别叫做txt被乘数,txt乘数,txt积……
    using System;
    using System.Windows.Forms;namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                this.txt被乘数.TextChanged += new EventHandler(被乘数或乘数_TextChanged);// 改变被乘数时触发事件
                this.txt乘数.TextChanged += new EventHandler(被乘数或乘数_TextChanged);// 改变乘数时触发事件
            }        void 被乘数或乘数_TextChanged(object sender, EventArgs e)
            {
                try
                {
                    decimal 被乘数 = Convert.ToDecimal(this.txt被乘数.Text);
                    decimal 乘数 = Convert.ToDecimal(this.txt乘数.Text);
                    this.txt积.Text = (被乘数 * 乘数).ToString();
                }
                catch
                {
                    this.txt积.Text = string.Empty;
                }
            }
        }
    }
      

  4.   

    TextChange中看两个文本框中是不是有值,且是数字,是的话相乘赋值就完了呗!