数独を解く(2日目)

ユーザーフォームを追加する
(input.cs)
Loadのイベントハンドラを作成する必要あり。

using System;
using System.Drawing;
using System.Windows.Forms;

namespace 数独をとく
{
    public partial class input : Form
    {
        Button[] button = new Button[9];
        public string num;

        public input()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterParent;
        }

        private void input_Load(object sender, EventArgs e)
        {
            this.SuspendLayout();
            this.FormBorderStyle = FormBorderStyle.None;
            this.Height = 250; this.Width = 250;

            int[] position = { 10, 90, 170 };
            for (int i = 0; i < 9; i++)
            {
                button[i] = new Button();
                button[i].Size = new Size(70, 70);
                button[i].Text = (i + 1).ToString();
                button[i].Font = new Font("Arial", 20);
                button[i].Location = new Point(position[i % 3], position[i / 3]);
                button[i].Click += button_Click;
            }
            this.Controls.AddRange(button);
            this.ResumeLayout();
        }
        private void button_Click(object sender, EventArgs e)
        {
            num = ((Button)sender).Text;
            this.Close();
        }
    }
}

その後メインフォームのIDbuttonのClickイベントハンドラを記述。

        private void IDbutton_Click(object sender, EventArgs e)
        {
            if (((Button)sender).Text == "")
            {
                input inputform = new input();
                inputform.ShowDialog();
                ((Button)sender).Text = inputform.num;
                inputform.Dispose();
            }
            else
            {
                ((Button)sender).Text = "";
            }
        }