为什么我的 C# 标签文本值不会更新?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15739038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why won't my C# label text value update?
提问by jrounsav
I have a c# program set up that is supposed to accept a quantity input if a checkbox is checked. It then multiplies the quantity by the price and updates the appropriate label with the total cost.
我设置了 ac# 程序,如果选中复选框,它应该接受数量输入。然后将数量乘以价格,并用总成本更新相应的标签。
However, when I run the program it does not update the label. I ran the debugger and the label's .text value in the system is correct but it still does not appear on the actual form.
但是,当我运行该程序时,它不会更新标签。我运行了调试器,系统中标签的 .text 值是正确的,但它仍然没有出现在实际表单上。
Is there a label property in Visual Studio that prevents changes from being rendered?
Visual Studio 中是否有阻止呈现更改的标签属性?
here is the snippet responsible for updating the label.Text value
这是负责更新 label.Text 值的代码段
if (chkSesame.Checked)
{
intSesameQty = Convert.ToInt32(txtSesameQty.Text);
decSesameTotal = intSesameQty * decBAGEL_PRICE;
lblSesameSeedTotal.Text = decSesameTotal.ToString("c");
}
采纳答案by dash
Without knowing more about the structure of your form, and how you are calling your code, it's hard to give you any other advice other than to attempt to call lblSesameSeedTotal.Refresh()
after setting the text.
在不了解表单结构以及如何调用代码的情况下,除了lblSesameSeedTotal.Refresh()
在设置文本后尝试调用之外,很难给您任何其他建议。
Calling Refresh (MSDN Control.Refreshlink) effectively invalidates the control and forces the runtime to redraw the control, which, of course, includes updating its text.
调用 Refresh(MSDN Control.Refresh链接)有效地使控件无效并强制运行时重绘控件,当然,这包括更新其文本。
There are lots of reasons why you may have to do this; redrawing is an expensive operation, so, in general, if you are handling an event elsewhere on the form, it may not update certain controls. This is especially true for labels and similar controls whose values tend to remain constant (e.g. a label for a textbox with the text: Enter Name Heredoesn't really need to change).
您可能必须这样做的原因有很多;重绘是一项开销很大的操作,因此,一般来说,如果您在窗体的其他地方处理事件,它可能不会更新某些控件。对于标签和其值往往保持不变的类似控件尤其如此(例如,带有文本的文本框的标签:在此处输入名称实际上不需要更改)。