I’ve just started programming meaning I’m new… I’ve made a tic tac toe game using else if statements.
The code below is code that will reset certain values within my game, I’m sure there’s but I can’t find an answer – How do I shorten this block of code?
btn1.Enabled = true;
btn2.Enabled = true;
btn3.Enabled = true;
btn4.Enabled = true;
btn5.Enabled = true;
btn6.Enabled = true;
btn7.Enabled = true;
btn8.Enabled = true;
btn9.Enabled = true;
btn1.Text = "";
btn2.Text = "";
btn3.Text = "";
btn4.Text = "";
btn5.Text = "";
btn6.Text = "";
btn7.Text = "";
btn8.Text = "";
btn9.Text = "";
btn1.BackColor = default(Color);
btn2.BackColor = default(Color);
btn3.BackColor = default(Color);
btn4.BackColor = default(Color);
btn5.BackColor = default(Color);
btn6.BackColor = default(Color);
btn7.BackColor = default(Color);
btn8.BackColor = default(Color);
btn9.BackColor = default(Color);
You should use an array, or better yet, a generic list which will hold all your buttons.
For example:
Then, you can iterate on the list like this:
The code above will run on all the buttons in the list and for each of them clear the text and set the BackColor.