目標』使被選定的方格內數字來源(位置i j)顯示清楚
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication17
{
public partial class Form1 : Form
{
Button[,] buttons = new Button[10, 10];
int[] array = new int[10];
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
int width, height;
width = this.Size.Width;
height = this.Size.Height;
for (int i = 0; i < 9; i++)
{
array[i] = i;
}
for (int j = 0; j < 4; j++)
{
for (int i = 0; i < 4; i++)
{
buttons[i, j] = new Button();
buttons[i, j].Location = new Point(j * 50, i * 50);
buttons[i, j].Text = (j * 4 + i).ToString();
this.Controls.Add(buttons[i, j]);
buttons[i, j].Click += new EventHandler(Button1_Click);
width = 50;
height = 50;
buttons[i, j].Size = new Size(width, height);
}
}
}
private void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (buttons[i, j].Text == " 0 ")
{
MessageBox.Show("i=" + i + ",j=" + j + " " + buttons[i, j].Text);
}
}
}
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);
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
int d1, tmp, k;
Random irand = new Random();
d1 = irand.Next(1, 10);
label1.Text = d1.ToString();
for (int j = 1; j < 10; j++)
{
k = 9 - j + 1;
tmp = array[d1];
array[d1] = array[k];
array[k] = tmp;
}
for (int j = 0; j < 3; j++)
{
for (int i = 0; i < 3; i++)
{
buttons[i, j].Location = new Point(j * 50, i * 50);
buttons[i, j].Text = array[i * 3 + j + 1].ToString();
this.Controls.Add(buttons[i, j]);
}
}
label2.Text = array[d1].ToString();
}
}
}