简体   繁体   English

如何在文本框C#中显示变量?

[英]how to display a variable in the text box C#?

hello i am facing a small problem and its by displaying a variable in the text box after checking another check box here is my code : 你好,我面临一个小问题,在选中另一个复选框后在文本框中显示变量,这是我的代码:

public partial class AccessoriesForm : Form
    {
        public AccessoriesForm()
        {
            InitializeComponent();
        }
        private void Total(object sender, EventArgs e)
        {
            double TotalPrice = 0;
            if (CagesCheckBox.Checked)
            {
                TotalPrice += 0.75;

                TotalPriceTextBox.Text = TotalPrice.ToString();
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

// please help // 请帮忙

I assume that your problem is you are defining TotalPrice inside of your method, so every time you see 0.75 in the TextBox .Put the definition outside of your method then it should work fine. 我假设您的问题是您在方法内部定义了TotalPrice ,因此每次在TextBox看到0.75 ,将定义放在方法之外即可正常工作。

double TotalPrice = 0;
private void Total(object sender, EventArgs e)
{
     if (CagesCheckBox.Checked)
     {
         TotalPrice += 0.75;

         TotalPriceTextBox.Text = TotalPrice.ToString();
     }
}

Also don't forget to attach your event handler.You can use CheckedChanged event for this and you can attach it in your constructor: 同样不要忘记附加事件处理程序。您可以为此使用CheckedChanged事件,并将其附加到构造函数中:

public AccessoriesForm()
{
     InitializeComponent();
     CagesCheckBox.CheckedChanged += Total;
}

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

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