
the image above is how the program looks
the four top buttons have been put in a 2d array as follows
private Button[,] btns;
public Form1()
{
InitializeComponent();
btns = new Button[,] { { button2 , button1 },
{ button4 , button3 }};
}
the four buttons have been initialized to
foreach (var btn in btns)
{
btn.Enabled = false;
}
and what i want to do is when you click the bottom two buttons each button in that row
enables and a color is signed (red and blue in turn like a connect 4 game)
i have managed to solve half of the problem but when i click the row1 button it keeps enabling row 2 as well and when i click on row2 button it starts enabling from row one
how can i restrict each button to deal with the 2d array so it only enables the correct rows.
here is the full code
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private Button[,] btns;
public Form1()
{
InitializeComponent();
btns = new Button[,] { { button2 , button1 },
{ button4 , button3 }};
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (var btn in btns)
{
btn.Enabled = false;
}
}
int cc = 0;
private void button5_Click(object sender, EventArgs e)
{
foreach (var btn in btns)
{
if (!btn.Enabled)
{
btn.Enabled = true;
if (cc == 0)
{
cc = 1;
btn.BackColor = Color.Red;
}
else
{
cc = 0;
btn.BackColor = Color.Blue;
}
return;
}
}
}
private void button6_Click(object sender, EventArgs e)
{
foreach (var btn in btns)
{
if (!btn.Enabled)
{
btn.Enabled = true;
if (cc == 0)
{
cc = 1;
btn.BackColor = Color.Red;
}
else
{
cc = 0;
btn.BackColor = Color.Blue;
}
return;
}
}
}
}
}
SOLVED
This problem is solved and this is how i solved it:
private void button5_Click(object sender, EventArgs e)
{
Button[] row1 = new Button[] {button2, button1};
foreach (var roww1 in row1)
{
if (!roww1.Enabled)
{
roww1.Enabled = true;
if (cc == 0)
{
cc = 1;
roww1.BackColor = Color.Red;
}
else
{
cc = 0;
roww1.BackColor = Color.Blue;
}
return;
}
}
}
When using a multidimensional array, you need to use a for loop instead of foreach to iterate through one row of the array at a time: