int a, b;
private void Form1_Load(object sender, EventArgs e)
{
this.Width = 600;
this.Height = 436;
for (a = 1; a <= 8; a++)
{
for (b = 1; b <= 8; b++)
{
Button btn = new Button();
btn.Name = (((a - 1) * 8) + b).ToString();
btn.Width = 50;
btn.Height = 50;
btn.Left = (b - 1) * 50;
btn.Top = (a - 1) * 50;
if ((a + b) % 2 == 0)
btn.BackColor = Color.WhiteSmoke;
开发者_开发技巧 else
btn.BackColor = Color.Black;
btn.Click += new EventHandler(btn_Click);
this.Controls.Add(btn);
}
}
}
int i, j,y;
void btn_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
if (radioButton1.Checked == true)
{
i = int.Parse(btn.Name);
j = i % 8;
for (y = 1; y <= 8; j+=8)
{
}
}
how can i change other buttons backcolor?
Make a List<Button>
and add your newly added buttons to this list. Then, you can change any property of button in this list with getting button's index
You'll need to keep a reference to the other buttons. Preferably: create all buttons, add them to a list. In the eventhandler, iterate that list and set the background color
Use your this.Controls
reference...not exactly syntactically correct....but you get the idea...
Button btn = (Button)sender;
if (radioButton1.Checked == true)
{
i = int.Parse(btn.Name);
j = i % 8;
for (y = 1; y <= 8; j+=8)
{
if(!btn.equals(this.Controls[y]))
this.Controls[y].BackColor = Color.Red;
}
}
精彩评论