Tudor P.
Tudor P.

Reputation: 27

c# values don't work correctly

I'm new to c# and I tried making a simple program. After I click the button, the values won't update with their actual value, so I have to click twice to make them actually work. Here's my code:

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

Upvotes: 0

Views: 110

Answers (1)

Sayse
Sayse

Reputation: 43300

You need to update the text after updating the value

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

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

Upvotes: 5

Related Questions