using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Button[,] buttons = new Button[17, 17]; //定義按鈕
int[] array = new int[17]; //定義按鈕代表數字
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Instantiating all the buttons in the array
for (int i = 0; i < 16; i++)
{
array[i] = i; //每個按鈕的值分別為0到15
}
/*
for (int j = 1; j < 10; j++)
{
buttons[1, j] = new Button();
buttons[1, j].Location = new Point(50 * j, 50);
buttons[1, j].Text = array[j].ToString();
this.Controls.Add(buttons[1, j]);
}
*/
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
buttons[i, j] = new Button(); //這行會生成4*4個按鈕
buttons[i, j].Location = new Point(j * 50, i * 50); // (i,j)按鈕位置以 (j*50,i*50) 分布
buttons[i, j].Text = j.ToString(); //按鈕Text只能顯示文字 所以iToString
buttons[i, j].Size = new Size(50, 50); //每個按鈕大小為50*50
this.Controls.Add(buttons[i, j]);
buttons[i, j].Click += new EventHandler(Button1_Click); //把生成的按鈕設成Button1
}
}
}
private void Button1_Click(object sender, EventArgs e) //這段是控制程式生成的按鈕Button1
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (sender == buttons[i, j])
{ MessageBox.Show("i=" + i + ",j=" + j + "," + buttons[i, j].Text); }
//點某個按鈕就會寫出它 i,j (位置) 跟按鈕的值
}
}
}
private void button1_Click(object sender, EventArgs e) //這段是控制自己放的按鈕 button1
{
int d1, tmp, k;
Random irand = new Random();
d1 = irand.Next(1, 17); //把d1隨機亂數為1到16的值
label1.Text = d1.ToString();
for (int j = 1; j < 17; j++)
{
k = 16 - j + 1;
tmp = array[d1];
array[d1] = array[k];
array[k] = tmp;
}
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
//buttons[i, j] = new Button();
buttons[i, j].Location = new Point(i * 50, j * 50);
buttons[i, j].Text = array[i*4+j+1].ToString();
this.Controls.Add(buttons[i, j]);
}
}
//buttons[1, 1].Text = array[1].ToString();
/*
for (int j = 1; j < 10; j++)
{
// buttons[1, j] = new Button();
buttons[1, j].Location = new Point(50 * j, 50);
buttons[1, j].Text = array[j].ToString();
this.Controls.Add(buttons[1, j]);
}
*/
label2.Text = array[d1].ToString();
}
}
}
沒有留言:
張貼留言