初学C#,菜鸟,求助。using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
     public partial class Form2 : Form
    {
        float a, b, c;
        public Form2()
        {
            InitializeComponent();
        }             private void label3_Click(object sender, EventArgs e)
        {        }        private void textBox1_TextChanged(object sender, EventArgs e)
        {        }        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            a=textBox1.Text;
            b=textBox2.Text;
            c = (1 - a) * (1 - a) + 100 * (b - a * a) * (b - a * a);
            }        private void textBox2_TextChanged(object sender, EventArgs e)
        {        }
    }
想在textBox1、textBox2中输入数据,经过运算后,在textBox3中输出,或者以按钮+文本框的形式输出。但是,好像输入的只是字符串型,不能进行运算,求支招!!
}

解决方案 »

  1.   

    加个 button ,点击事件里
    int a = int.Parse(textBox1.Text);
    int b = int.Parse(textBox2.Text);
    int c = (1 - a) * (1 - a) + 100 * (b - a * a) * (b - a * a);
    textBox3.Text = c.ToString();
      

  2.   

    如果能保证输入的字符串格式是正确的,可以用a = float.Parse(textBox1.Text)或者a = Convert.ToSingle(textBox1.Text)来转换。
      

  3.   

    private void button1_Click(object sender, EventArgs e){double a = Convert.ToDouble(textBox1.Text);
    double b = Convert.ToDouble(textBox2.Text);
    double c = (1 - a) * (1 - a) + 100 * (b - a * a) * (b - a * a);
    textBox3.Text = c.ToString();}
      

  4.   

    private void button1_Click(object sender, EventArgs e)
    {
        double a = Convert.ToDouble(textBox1.Text);
        double b = Convert.ToDouble(textBox2.Text);
        double c = (1 - a) * (1 - a) + 100 * (b - a * a) * (b - a * a);
        textBox3.Text = c.ToString();
    }
      

  5.   

    decimal a = decimal.Parse(textBox1.Text);
    decimal b = decimal.Parse(textBox2.Text);
    decimal c = (1 - a) * (1 - a) + 100 * (b - a * a) * (b - a * a);
    textBox3.Text = c.ToString();
      

  6.   

    在textBox的KeyPress 检查输入的是否为数字
            private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (e.KeyChar != '\b')//这是允许输入退格键
                {
                    if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字
                    {
                        e.Handled = true;
                    }
                }
            }
      

  7.   


    上面 的方法最好用Try catch 捕获一下,不然输入非数字会报错的
      

  8.   


    还有KeyPress是在哪里调出来的?我双击textBox之后,出现的是text_Changed()
      

  9.   

    选择 TextBox ,查看“属性”页,“事件”选项卡,找到 KeyPress ,双击它
    或者在构造函数里加上
    this.textBox1.KeyPress += new KeyPressEventHandler(textBox2_KeyPress);
    然后照搬9楼的方法
      

  10.   

    请问,是在textBox1里边加this.textBox1.KeyPress += new KeyPressEventHandler(textBox2_KeyPress);还是在textBox2里边加?9楼的代码又是填在哪?我试过了好几种方法,都是有问题,运行不了
      

  11.   

    构造函数最后一行
    this.你的TextBox名字.KeyPress += new KeyPressEventHandler(textBox2_KeyPress);
    然后
    private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar != '\b')//这是允许输入退格键
        {
            if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字
            {
                e.Handled = true;
            }
        }
    }这整段包括方法名一字不用改放到类里,完了