简体   繁体   English

为什么我的C#标签文本值不会更新?

[英]Why won't my C# label text value update?

I have ac# program set up that is supposed to accept a quantity input if a checkbox is checked. 我有一个ac#程序设置,如果选中了复选框,它应该接受数量输入。 It then multiplies the quantity by the price and updates the appropriate label with the total cost. 然后,它将数量乘以价格,并使用总成本更新相应的标签。

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");
    }

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.Refresh link) effectively invalidates the control and forces the runtime to redraw the control, which, of course, includes updating its text. 调用刷新(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 (eg a label for a textbox with the text: Enter Name Here doesn't really need to change). 对于其值往往保持不变的标签和类似控件尤其如此(例如带有文本的文本框的标签: 输入名称此处并不需要更改)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM