public Form1()
{
InitializeComponent();
摆放按钮();
}
private void 摆放按钮()
{
int btnNums = 通关数 + 基数;
int wid =Convert.ToInt32 (bigWid / btnNums);
Point p = new Point(0, 0);
for (int i = 0; i != btnNums; i++)
{
p.X = Convert.ToInt32(x0 + wid * i*0.88);
for (int j= 0; j!= btnNums;j++)
{
Button btn = new Button();//实例化一个button
btn.Name = "btn" + (i+1).ToString()+(j+1).ToString();
btn.Text = btn.Name;
btn.Width =Convert.ToInt32(wid *0.85); btn.Height = btn.Width;
p.Y= Convert.ToInt32(y0 + wid * j*0.88);
btn.Location = p;
this.Controls.Add(btn);
btn.Click += new EventHandler(aBtn_Click); }
}
}
private void aBtn_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
string str = btn.Text.Substring(3);
int midL= str.Length / 2;
int H= Convert.ToInt32(str.Substring(0,midL));
int L= Convert.ToInt32(str.Substring( midL));
//MessageBox.Show(idx.ToString());
btn.BackColor = Color.Black;
foreach (Control ctl in this.Controls)
{
if(ctl is Button)
{
if (ctl.Text == "btn" + (H - 1).ToString() + L.ToString())
ctl.BackColor = Color.Black;
if (ctl.Text == "btn" + (H + 1).ToString() + L.ToString())
ctl.BackColor = Color.Black;
if (ctl.Text == "btn" + H .ToString() + (L-1).ToString())
ctl.BackColor = Color.Black;
if (ctl.Text == "btn" + H .ToString() + (L+1).ToString())
ctl.BackColor = Color.Black;
}
}
清场重来();
} private void 清场重来()
{
int cntColor=0;
foreach (Control ctl in this.Controls)
{
if (ctl is Button)
{
if (ctl.BackColor == Color.Black) cntColor++;
}
}
if (cntColor == (通关数 + 基数) * (通关数 + 基数))
{
通关数++;
//清空按钮;
foreach (Control ctl in this.Controls)
{
if (ctl is Button) ctl.Dispose();
}
摆放按钮();
}
}
{
InitializeComponent();
摆放按钮();
}
private void 摆放按钮()
{
int btnNums = 通关数 + 基数;
int wid =Convert.ToInt32 (bigWid / btnNums);
Point p = new Point(0, 0);
for (int i = 0; i != btnNums; i++)
{
p.X = Convert.ToInt32(x0 + wid * i*0.88);
for (int j= 0; j!= btnNums;j++)
{
Button btn = new Button();//实例化一个button
btn.Name = "btn" + (i+1).ToString()+(j+1).ToString();
btn.Text = btn.Name;
btn.Width =Convert.ToInt32(wid *0.85); btn.Height = btn.Width;
p.Y= Convert.ToInt32(y0 + wid * j*0.88);
btn.Location = p;
this.Controls.Add(btn);
btn.Click += new EventHandler(aBtn_Click); }
}
}
private void aBtn_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
string str = btn.Text.Substring(3);
int midL= str.Length / 2;
int H= Convert.ToInt32(str.Substring(0,midL));
int L= Convert.ToInt32(str.Substring( midL));
//MessageBox.Show(idx.ToString());
btn.BackColor = Color.Black;
foreach (Control ctl in this.Controls)
{
if(ctl is Button)
{
if (ctl.Text == "btn" + (H - 1).ToString() + L.ToString())
ctl.BackColor = Color.Black;
if (ctl.Text == "btn" + (H + 1).ToString() + L.ToString())
ctl.BackColor = Color.Black;
if (ctl.Text == "btn" + H .ToString() + (L-1).ToString())
ctl.BackColor = Color.Black;
if (ctl.Text == "btn" + H .ToString() + (L+1).ToString())
ctl.BackColor = Color.Black;
}
}
清场重来();
} private void 清场重来()
{
int cntColor=0;
foreach (Control ctl in this.Controls)
{
if (ctl is Button)
{
if (ctl.BackColor == Color.Black) cntColor++;
}
}
if (cntColor == (通关数 + 基数) * (通关数 + 基数))
{
通关数++;
//清空按钮;
foreach (Control ctl in this.Controls)
{
if (ctl is Button) ctl.Dispose();
}
摆放按钮();
}
}
{
public partial class Form1 : Form
{
public int 通关数 = 0;
public int 基数= 2;
public int x0=125;
public int y0 = 25;
public int bigWid= 500; public Form1()
{
InitializeComponent();
摆放按钮();
}
private void 摆放按钮()
{
int btnNums = 通关数 + 基数;
int wid =Convert.ToInt32 (bigWid / btnNums);
Point p = new Point(0, 0);
for (int i = 0; i != btnNums; i++)
{
p.X = Convert.ToInt32(x0 + wid * i*0.88);
for (int j= 0; j!= btnNums;j++)
{
Button btn = new Button();//实例化一个button
btn.Name = "btn" + (i+1).ToString()+(j+1).ToString();
btn.Text = btn.Name;
// btn.Tag = i;//tag是控件留给用户自己定义的一个数据项,可以是任何数据类型包括object
btn.Width =Convert.ToInt32(wid *0.85); btn.Height = btn.Width;
p.Y= Convert.ToInt32(y0 + wid * j*0.88);
btn.Location = p;
this.Controls.Add(btn);//向具体的控件中添加button
btn.Click += new EventHandler(aBtn_Click);//使用事件函数句柄指向一个具体的函数
}
}
}
private void aBtn_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
string str = btn.Text.Substring(3);
int midL= str.Length / 2;
int H= Convert.ToInt32(str.Substring(0,midL));
int L= Convert.ToInt32(str.Substring( midL));
//MessageBox.Show(idx.ToString());
btn.BackColor = Color.Black;
foreach (Control ctl in this.Controls)
{
if(ctl is Button)
{
if (ctl.Text == "btn" + (H - 1).ToString() + L.ToString())
ctl.BackColor = Color.Black;
if (ctl.Text == "btn" + (H + 1).ToString() + L.ToString())
ctl.BackColor = Color.Black;
if (ctl.Text == "btn" + H .ToString() + (L-1).ToString())
ctl.BackColor = Color.Black;
if (ctl.Text == "btn" + H .ToString() + (L+1).ToString())
ctl.BackColor = Color.Black;
}
}
清场重来();
} private void 清场重来()
{
int cntColor=0;
foreach (Control ctl in this.Controls)
{
if (ctl is Button)
{
if (ctl.BackColor == Color.Black) cntColor++;
}
}
if (cntColor == (通关数 + 基数) * (通关数 + 基数))
{
通关数++;
//清空按钮;
foreach (Control ctl in this.Controls)
{
if (ctl is Button) ctl.Dispose();
}
摆放按钮();
}
} private void label1_Click(object sender, EventArgs e)
{
}
}
}
if (ctl is Button) ctl.ControlRemoved();错误 CS0079 事件“Control.ControlRemoved”只能出现在 += 或 -= 的左边
{
if (this.Controls[i] is Button)
{
this.Controls.RemoveAt(i);
i--;
}
}