C#->;如何实时读取和打印文本框中的值
如何从文本框中获取值并立即在其他文本框中显示计算结果 我想加载值并根据公式在其他文本框中进行更改 这段代码不是这样工作的,或者只计算平均ap,但只有在文本框中键入任何字符后,才会显示结果。所有其他文本框都是不可重写的0或NaNC#->;如何实时读取和打印文本框中的值,c#,C#,如何从文本框中获取值并立即在其他文本框中显示计算结果 我想加载值并根据公式在其他文本框中进行更改 这段代码不是这样工作的,或者只计算平均ap,但只有在文本框中键入任何字符后,才会显示结果。所有其他文本框都是不可重写的0或NaN using System; using System.Drawing; using System.Windows.Forms; namespace sustruzenie_frezovanie { /// <summary> /// Des
using System;
using System.Drawing;
using System.Windows.Forms;
namespace sustruzenie_frezovanie
{
/// <summary>
/// Description of sustruzenie.
/// </summary>
public partial class sustruzenie : Form
{
double d = 0;
double dm = 0;
double vc = 0;
double f = 0;
double n = 0;
double ap = 0;
double vf = 0;
public const double PI = 3.1415926535897931;
public sustruzenie()
{
InitializeComponent();
}
void Label1Click(object sender, EventArgs e)
{
}
void TextBox1TextChanged(object sender, EventArgs e)
{
double.TryParse(textBox1.Text, out d);
d = double.Parse(textBox1.Text);
}
void TextBox2TextChanged(object sender, EventArgs e)
{
double.TryParse(textBox2.Text, out dm);
dm = double.Parse(textBox2.Text);
}
void TextBox3TextChanged(object sender, EventArgs e)
{
double.TryParse(textBox3.Text, out vc);
vc = double.Parse(textBox3.Text);
if(d >=0 && n >= 0)
{
VypocetVc();
}
}
void TextBox4TextChanged(object sender, EventArgs e)
{
double.TryParse(textBox4.Text, out vf);
vf = double.Parse(textBox4.Text);
if(f >=0 && n >= 0)
{
VypocetVf();
}
}
void TextBox5TextChanged(object sender, EventArgs e)
{
double.TryParse(textBox5.Text, out n);
n = double.Parse(textBox5.Text);
if(vc >=0 && d >= 0)
{
VypocetN();
}
}
void TextBox6TextChanged(object sender, EventArgs e)
{
double.TryParse(textBox6.Text, out f);
f = double.Parse(textBox6.Text);
if(vf >=0 && n >= 0)
{
VypocetF();
}
}
void TextBox7TextChanged(object sender, EventArgs e)
{
double.TryParse(textBox7.Text, out ap);
ap = double.Parse(textBox7.Text);
if(d >=0 && dm >= 0)
{
VypocetAp();
}
}
void SustruzenieTextChanged(object sender, EventArgs e)
{
}
private void VypocetVc()
{
textBox3.Text = ((PI * d * n)/100).ToString();
}
private void VypocetVf()
{
textBox4.Text = (f * n).ToString();
}
private void VypocetN()
{
textBox5.Text = ((vc * 1000)/(PI * d)).ToString();
}
private void VypocetF()
{
textBox6.Text = (vf/n).ToString();
}
private void VypocetAp()
{
textBox7.Text = ((d - dm)/2).ToString();
}
}
}
使用系统;
使用系统图;
使用System.Windows.Forms;
命名空间sustruzenie_frezovanie
{
///
///对苏斯特鲁泽尼的描述。
///
公共部分类sustruzenie:表单
{
双d=0;
双dm=0;
双vc=0;
双f=0;
双n=0;
双ap=0;
双vf=0;
公共常数双PI=3.1415926535897931;
公众支持()
{
初始化组件();
}
void Label1Click(对象发送方,事件参数e)
{
}
void TextBox1TextChanged(对象发送方,事件参数e)
{
double.TryParse(textBox1.Text,out d);
d=double.Parse(textBox1.Text);
}
void TextBox2TextChanged(对象发送方,事件参数e)
{
double.TryParse(textBox2.Text,out dm);
dm=double.Parse(textBox2.Text);
}
void TextBox3TextChanged(对象发送方,事件参数e)
{
double.TryParse(textBox3.Text,out-vc);
vc=double.Parse(textBox3.Text);
如果(d>=0&&n>=0)
{
VypocetVc();
}
}
void TextBox4TextChanged(对象发送方,事件参数e)
{
double.TryParse(textBox4.Text,out vf);
vf=double.Parse(textBox4.Text);
如果(f>=0&&n>=0)
{
VypocetVf();
}
}
void TextBox5TextChanged(对象发送方,事件参数e)
{
double.TryParse(textBox5.Text,out n);
n=double.Parse(textBox5.Text);
如果(vc>=0&&d>=0)
{
VypocetN();
}
}
void TextBox6TextChanged(对象发送方,事件参数e)
{
double.TryParse(textBox6.Text,out f);
f=double.Parse(textBox6.Text);
如果(vf>=0&&n>=0)
{
VypocetF();
}
}
void TextBox7TextChanged(对象发送方,事件参数e)
{
double.TryParse(textBox7.Text,out-ap);
ap=double.Parse(textBox7.Text);
如果(d>=0&&dm>=0)
{
VypocetAp();
}
}
void SUSTRUZENITEXTCHANGED(对象发送方,事件参数e)
{
}
私有void VypocetVc()
{
Text=((PI*d*n)/100.ToString();
}
私有void VypocetVf()
{
textBox4.Text=(f*n).ToString();
}
私有void VypocetN()
{
textBox5.Text=((vc*1000)/(PI*d)).ToString();
}
私有void VypocetF()
{
textBox6.Text=(vf/n).ToString();
}
私有void VypocetAp()
{
Text=((d-dm)/2.ToString();
}
}
}
您可以对每个文本框使用文本更改事件
确定,如果您想实时执行,则需要与文本框
关联的文本更改事件
假设要将两个文本框中的值相加,并立即在第三个文本框中显示结果。你可以这样做
public partial class Form1 : Form
{
double value1 = 0; // variables
double value2 = 0; // variables
public Form1()
{
InitializeComponent();
}
// TextChanged associated with textBox1
private void textBox1_TextChanged(object sender, EventArgs e)
{
// Use TryParse instead of Prase. TryParse will not read if textBox is empty
double.TryParse(textBox1.Text, out value1);
if(value1 >=0 && value2 >= 0)
{
AddUp();// calling the metod with addition formula
}
}
// TextChanged associated with textBox2
private void textBox2_TextChanged(object sender, EventArgs e)
{
// Use TryParse instead of Prase. TryParse will not read if textBox is empty
double.TryParse(textBox2.Text, out value2);
if (value1 >= 0 && value2 >= 0)
{
AddUp();// calling the metod with addition formula
}
}
private void AddUp()
{
textBox3.Text = (value1+value2).ToString();
}
我希望这个答案能回答你的问题。Otherwirse在注释部分写入。进行计算()并设置为TextBox输入的另一个内部TextChanged处理程序。每一个文本框都有文本变化,尽管我试着按照你说的做,但只有一个公式对我有效。其他值的永久值为0或NaN。此外,在要显示结果的文本框中,需要添加一个字符来显示结果,否则为空。我也会发送代码,但这在这个评论中是不可能的。@RadoslavDugas将您的代码加载到GitHub之类的地方,并在这里粘贴下载链接。