问题描述
我的第一个帖子在这里.
my first post here.
我正在构建一个计算器,该计算器通过文本框接收用户输入并将其转换为双精度并将这些数字传递到包含公式的自定义类中(完成后答案将转换回字符串).我已完成所有工作,但我希望带有答案的标签在填写文本框后即可自动更新.单击后,标签文本输出是正确的.
I'm building a calculator that takes user input via text box and converts to a double and passes those numbers into a custom class that contains a formula(the answer is converted back to a string once complete). I have everything working, but I would like the label with the answer to automatically update once the text boxes are filled out. The label text output is correct once clicked on.
这是到目前为止我为标签使用的代码...
heres the code I have for the label so far...
private void lblUtil1_Click(object sender, EventArgs e)
{
dblUtil1 = Tinseth.Bigness(dblSG) * Tinseth.BTFactor(dblBT1);
double UtilRounded1 = Math.Round(dblUtil1 * 100);
lblUtil1.Text = UtilRounded1.ToString() + "%";
}
是否可以检测所有相关字段是否已完成,或者是否需要循环?我非常感谢所有帮助,并希望成为这个社区的一份子!
Is there something that can detect if all the pertinent fields are completed or will this require a loop? I greatly appreciate all help and look forward to being a part of this community!
growthtek
growthtek
推荐答案
一种方法是做到这一点:
One approach would be to make this:
private void lblUtil1_Click(object sender, EventArgs e)
{
dblUtil1 = Tinseth.Bigness(dblSG) * Tinseth.BTFactor(dblBT1);
double UtilRounded1 = Math.Round(dblUtil1 * 100);
lblUtil1.Text = UtilRounded1.ToString() + "%";
}
共享方法:
private void Calculate();
{
dblUtil1 = Tinseth.Bigness(dblSG) * Tinseth.BTFactor(dblBT1);
double UtilRounded1 = Math.Round(dblUtil1 * 100);
lblUtil1.Text = UtilRounded1.ToString() + "%";
}
并摆脱Click
事件.然后使用文本框Validated
事件:
and get rid of the Click
event. Then consume the text boxes Validated
event:
private void TextBox_Validated(object sender, EventArgs e)
{
Calculate();
}
现在将所有文本框Validated
事件挂接到同一处理程序.现在,当用户离开文本框时,它将自动进行计算.
Now hook all of the text boxes Validated
events up to this same handler. Now when the user leaves the text box, it will calculate automatically.
Validated
事件也可以很容易地成为TextChanged
事件.他们每次键入数字时,都会进行计算.但这可能太频繁了.
The Validated
event could just as easily be the TextChanged
event as well. That will calculate every time they type a number. But that's probably too frequent.
这篇关于C#.如何使用将多个文本框传递到方法中并自动刷新标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!