上图是程序的外观
四个顶部按钮已按如下方式放置在二维数组中
private Button[,] btns;
public Form1()
{
InitializeComponent();
btns = new Button[,] { { button2 , button1 },
{ button4 , button3 }};
}
四个按钮已初始化为
foreach (var btn in btns)
{
btn.Enabled = false;
}
我想要做的是当您单击底部的两个按钮时,该行中的每个按钮都启用并签名颜色(红色和蓝色依次像连接 4 游戏)
我已经设法解决了一半的问题,但是当我单击第 1 行按钮时,它也会继续启用第 2 行,当我单击第 2 行按钮时,它会从第 1 行开始启用
我如何限制每个按钮来处理二维数组,以便它只启用正确的行。
这是完整的代码
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;
}
}
}
}
}
解决了
这个问题解决了,这就是我解决它的方法:
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;
}
}
}