c#值无法正常工作

时间:2013-09-13 14:52:28

标签: c# checkbox

我是c#的新手,我尝试制作一个简单的程序。单击按钮后,值将不会更新其实际值,因此我必须单击两次才能使它们真正起作用。这是我的代码:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private static int p;
        private static int money;

        public Form1()
        {
            InitializeComponent();

            p = 0;
            money = 100;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            m.Text = money.ToString();
            ex.Text = p.ToString();

            if (checkBox1.Checked && checkBox2.Checked)
            {
                MessageBox.Show("You cannot select both.", "Nope");
            }
            else if (checkBox1.Checked)
            {
                p += 2;
            }
            else if (checkBox2.Checked)
            {
                money -= 50;
                p += 10;
            }
            else
            {
                return;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:5)

您需要在更新值

后更新文本
if (checkBox1.Checked & checkBox2.Checked)
{
    MessageBox.Show("You cannot select both.", "Nope");
}
....

 m.Text = money.ToString();
 ex.Text = p.ToString();