初学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中输出,或者以按钮+文本框的形式输出。但是,好像输入的只是字符串型,不能进行运算,求支招!!
}
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中输出,或者以按钮+文本框的形式输出。但是,好像输入的只是字符串型,不能进行运算,求支招!!
}
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();
double b = Convert.ToDouble(textBox2.Text);
double c = (1 - a) * (1 - a) + 100 * (b - a * a) * (b - a * a);
textBox3.Text = c.ToString();}
{
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();
}
decimal b = decimal.Parse(textBox2.Text);
decimal c = (1 - a) * (1 - a) + 100 * (b - a * a) * (b - a * a);
textBox3.Text = c.ToString();
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar != '\b')//这是允许输入退格键
{
if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字
{
e.Handled = true;
}
}
}
上面 的方法最好用Try catch 捕获一下,不然输入非数字会报错的
还有KeyPress是在哪里调出来的?我双击textBox之后,出现的是text_Changed()
或者在构造函数里加上
this.textBox1.KeyPress += new KeyPressEventHandler(textBox2_KeyPress);
然后照搬9楼的方法
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;
}
}
}这整段包括方法名一字不用改放到类里,完了