close
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Button[,] Buttons = new System.Windows.Forms.Button[5, 5];
public System.Windows.Forms.Button Button1;
Random rand = new Random();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Button1 = new System.Windows.Forms.Button();
Button1.Location = new Point(400, 400);
Button1.Size = new Size(100, 30);
Button1.Text = "Rand";
this.Controls.Add(Button1);
for (int j = 1; j < 5; ++j)
{
for (int i = 1; i < 5; i++)
{
Buttons[i, j] = new Button();
Buttons[i, j].Size = new Size(50, 50);
Buttons[i, j].Location = new Point(i * 50, j * 50);
this.Controls.Add(Buttons[i, j]);//出現在畫面中
}
}
}
private void button1_Click(object sender, EventArgs e)
{
int x, y, a;
int[] myarray = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,16 };
for (int i = 15; i > 1; i--)
{
x = rand.Next(0, i);
y = myarray[x];
myarray[x] = myarray[i];
myarray[i] = y;
}
for (int j = 1; j < 5; j++)
{
for (int i = 1; i < 5; i++)
{
a = myarray[(j - 1) * 4 + (i - 1)];
Buttons[i, j].Text = Convert.ToString(a);
this.Controls.Add(Buttons[i, j]);
}
}
}
}
}
全站熱搜
留言列表