C#里如何实时计算更新?

来源:百度知道 编辑:UC知道 时间:2024/05/13 11:49:40
不是这样问和不合理。

给三个textbox,分别对应a,b,c,在a与b上填写上数字,即可在c上显示ab的乘积结果。

用鼠标移走事件与焦点丢失事件都不是很理想,无法达到实时的效果~

可以实现么?

在winform下,知道的朋友请回答一下,谢谢!
知道原理,可不知道代码怎么写,都没接触过Timer,希望大家给一下代码。

用TextChanged事件
如果TextBox1代表a,TextBox2代表b,TextBox3代表c,则:

private void textBox1_TextChanged(object sender, EventArgs e)
{
Int32 a,b;
if (isNumberic(textBox1.Text,out a) && isNumberic(textBox2.Text, out b))
textBox3.Text = (a*b).ToString();
}

private void textBox2_TextChanged(object sender, EventArgs e)
{
Int32 a, b;
if (isNumberic(textBox1.Text, out a) && isNumberic(textBox2.Text, out b))
textBox3.Text = (a * b).ToString();

}

protected bool isNumberic(string message, out int result)
{
System.Text.RegularExpressions.Regex rex =
new System.Text.RegularExpressions.Regex(@"^\d+$");
result = -1;
if (rex.IsMatch(message))
{
result = i