OK,让我们说,我们有以下列形式5x5的C# - 单击时改变相邻的按钮颜色
[11] [12] [13] [14] [15]
[21] [22] [23] [24] [25]
[31] [32] [33] [34] [35]
[41] [42] [43] [44] [45]
[51] [52] [53] [54] [55]
按钮而想举例来说,如果我按一下按钮34,改变按钮24的颜色,44,33和35. 问题是我不知道如何做到这一点,当我点击一个按钮来返回任何这些值。 拿一个我想要做的事情的伪代码。
When I press the button [i, j]:
Change color of button[i-1, j];
Change color of button[i +1, j];
Change color of button[i, j +1];
Change color of button[i, j-1];
所以,当我按下一个按钮,我需要以某种方式获得按钮的值I,J,我可以在相同的风格(我+1,J等)办理其他按钮
我可以分别对待每一个按钮.....
private void button1_Click(object sender, EventArgs e)
{ // change colors
}
private void button2_Click(object sender, EventArgs e)
{ // change colors
}
private void button3_Click(object sender, EventArgs e)
{ // change colors
}
....
private void button25_Click(object sender, EventArgs e)
{ // change colors
}
,......但我有25个按钮,我不想写相同的代码在所有的25个按键。
我想要的是:
private void button[i][j]_Click(object sender, EventArgs e)
{
button[i+1][j].BackColor = Color.Red;
button[i][j+1].BackColor ...... etc. etc.
// I know is incorrect, but is just how I want to act, if you understand me.
}
原谅我的英语不好。
使用数组之前空,相同的事件处理程序添加到每个按钮。 – 2013-02-24 20:43:48
它可能会帮助你得到更多的答案,如果你放置额外的标签,指定更多的发展类型,即winforms或asp.net webforms ... – 2013-02-24 20:56:09