2010年10月29日 星期五

2010/10/29 數字盤完成

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        System.Windows.Forms.Button[] Button1;
        System.Windows.Forms.TextBox[] TextBox1;
        int[,] a = new int[4, 4];
        int bno;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Button1 = new System.Windows.Forms.Button[9];
            TextBox1 = new System.Windows.Forms.TextBox[9];
            for (int i = 0; i < 9; ++i)
            {
                Button1[i] = new Button();
                Button1[i].Click += new System.EventHandler(Button1_Click);
                this.Controls.Add(Button1[i]);
                if (i <= 2)
                    Button1[i].Location = new System.Drawing.Point(10 + i * 80, 10 + 100);
                else if (i > 2 && i <= 5)
                    Button1[i].Location = new System.Drawing.Point(10 + (i - 3) * 80, 40 + 100);
                else if (i > 5 && i <= 9)
                    Button1[i].Location = new System.Drawing.Point(10 + (i - 6) * 80, 70 + 100);
            }
            for (int i = 0; i < 9; ++i)
            {
                TextBox1[i] = new TextBox();
                this.Controls.Add(TextBox1[i]);
                if (i <= 2)
                    TextBox1[i].Location = new System.Drawing.Point(10 + i * 100, 10);
                else if (i > 2 && i <= 5)
                    TextBox1[i].Location = new System.Drawing.Point(10 + (i - 3) * 100, 40);
                else if (i > 5 && i <= 9)
                    TextBox1[i].Location = new System.Drawing.Point(10 + (i - 6) * 100, 70);
            }
            TextBox1[0].Text = "7";
            TextBox1[1].Text = "2";
            TextBox1[2].Text = "4";
            TextBox1[3].Text = "5";
            TextBox1[4].Text = "0";
            TextBox1[5].Text = "6";
            TextBox1[6].Text = "8";
            TextBox1[7].Text = "3";
            TextBox1[8].Text = "1";
        }
        public void Button1_Click(object sender, EventArgs e)
        {
            // System.Windows.Forms.MessageBox.Show("You have clicked button " +
            //   ((System.Windows.Forms.Button)sender).Tag.ToString());
            //MessageBox.Show(Convert.ToString(((Button)sender).Text));
            String tnum = sender.ToString();
            int tlen = tnum.Length;
            String no = tnum.Substring(tlen - 1);
            for (int i = 0; i < 9; i++)
            {
                if (Button1[i].Text == no)
                   // MessageBox.Show("" + no);
                bno = i;
            }
            // MessageBox.Show(""+bno);
             switch (bno)
             {
                 case 0:
                    // MessageBox.Show("0");
                     if (Button1[1].Text == "0")
                     {
                         String temp;
                         temp = Button1[1].Text;
                         Button1[1].Text = Button1[0].Text;
                         Button1[0].Text = temp;
                     }
                     else if (Button1[3].Text == "0")
                     {
                         String temp;
                         temp = Button1[3].Text;
                         Button1[3].Text = Button1[0].Text;
                         Button1[0].Text = temp;
                     }
                     break;
                 case 1:
                    // MessageBox.Show("1");
                     if (Button1[4].Text =="0")
                     {
                         String temp;
                         temp = Button1[4].Text;
                         Button1[4].Text = Button1[1].Text;
                         Button1[1].Text = temp;              
                     }
                     else if (Button1[0].Text == "0")
                     {
                         String temp;
                         temp = Button1[0].Text;
                         Button1[0].Text = Button1[1].Text;
                         Button1[1].Text = temp;
                     }
                     else if (Button1[2].Text == "0")
                     {
                         String temp;
                         temp = Button1[2].Text;
                         Button1[2].Text = Button1[1].Text;
                         Button1[1].Text = temp;
                     }
                    
                     break;
                 case 2:
                    // MessageBox.Show("2");
                     if (Button1[1].Text == "0")
                     {
                         String temp;
                         temp = Button1[1].Text;
                         Button1[1].Text = Button1[2].Text;
                         Button1[2].Text = temp;
                     }
                     else if (Button1[5].Text == "0")
                     {
                         String temp;
                         temp = Button1[5].Text;
                         Button1[5].Text = Button1[2].Text;
                         Button1[2].Text = temp;
                     }
                     break;
                 case 3:
                     //MessageBox.Show("3");
                     if (Button1[0].Text == "0")
                     {
                         String temp;
                         temp = Button1[0].Text;
                         Button1[0].Text = Button1[3].Text;
                         Button1[3].Text = temp;
                     }
                     else if (Button1[4].Text == "0")
                     {
                         String temp;
                         temp = Button1[4].Text;
                         Button1[4].Text = Button1[3].Text;
                         Button1[3].Text = temp;
                     }
                     else if (Button1[6].Text == "0")
                     {
                         String temp;
                         temp = Button1[6].Text;
                         Button1[6].Text = Button1[3].Text;
                         Button1[3].Text = temp;
                     }
                     break;
                 case 4:
                     //MessageBox.Show("4");
                     if (Button1[1].Text == "0")
                     {
                         String temp;
                         temp = Button1[4].Text;
                         Button1[4].Text = Button1[1].Text;
                         Button1[1].Text = temp;
                     }
                     else if (Button1[3].Text == "0")
                     {
                         String temp;
                         temp = Button1[3].Text;
                         Button1[3].Text = Button1[4].Text;
                         Button1[4].Text = temp;
                     }
                     else if (Button1[5].Text == "0")
                     {
                         String temp;
                         temp = Button1[5].Text;
                         Button1[5].Text = Button1[4].Text;
                         Button1[4].Text = temp;
                     }
                     else if (Button1[7].Text == "0")
                     {
                         String temp;
                         temp = Button1[7].Text;
                         Button1[7].Text = Button1[4].Text;
                         Button1[4].Text = temp;
                     }
                     break;
                 case 5:
                    // MessageBox.Show("5");
                     if (Button1[2].Text == "0")
                     {
                         String temp;
                         temp = Button1[2].Text;
                         Button1[2].Text = Button1[5].Text;
                         Button1[5].Text = temp;
                     }
                     else if (Button1[4].Text == "0")
                     {
                         String temp;
                         temp = Button1[4].Text;
                         Button1[4].Text = Button1[5].Text;
                         Button1[5].Text = temp;
                     }
                     else if (Button1[8].Text == "0")
                     {
                         String temp;
                         temp = Button1[8].Text;
                         Button1[8].Text = Button1[5].Text;
                         Button1[5].Text = temp;
                     }
                     break;
                 case 6:
                   //  MessageBox.Show("6");
                     if (Button1[3].Text == "0")
                     {
                         String temp;
                         temp = Button1[3].Text;
                         Button1[3].Text = Button1[6].Text;
                         Button1[6].Text = temp;
                     }
                     else if (Button1[7].Text == "0")
                     {
                         String temp;
                         temp = Button1[7].Text;
                         Button1[7].Text = Button1[6].Text;
                         Button1[6].Text = temp;
                     }
                     break;
                 case 7:
                    // MessageBox.Show("7");
                     if (Button1[4].Text == "0")
                     {
                         String temp;
                         temp = Button1[4].Text;
                         Button1[4].Text = Button1[7].Text;
                         Button1[7].Text = temp;
                     }
                     else if (Button1[6].Text == "0")
                     {
                         String temp;
                         temp = Button1[6].Text;
                         Button1[6].Text = Button1[7].Text;
                         Button1[7].Text = temp;
                     }
                     else if (Button1[8].Text == "0")
                     {
                         String temp;
                         temp = Button1[8].Text;
                         Button1[8].Text = Button1[7].Text;
                         Button1[7].Text = temp;
                     }
                     break;
                 case 8:
                   //  MessageBox.Show("8");
                     if (Button1[5].Text == "0")
                     {
                         String temp;
                         temp = Button1[5].Text;
                         Button1[5].Text = Button1[8].Text;
                         Button1[8].Text = temp;
                     }
                     else if (Button1[7].Text == "0")
                     {
                         String temp;
                         temp = Button1[7].Text;
                         Button1[7].Text = Button1[8].Text;
                         Button1[8].Text = temp;
                     }
                     break;
             }

           
        }
        private void button2_Click(object sender, EventArgs e)
        {
           
            }
        private void button1_Click_1(object sender, EventArgs e)
        {
            //輸入
            for (int j = 0; j < 9; j++)
            {
                if (j < 3)
                {
                    a[1, j + 1] = Convert.ToInt16(TextBox1[j].Text);
                }
                else if (j >= 3 && j < 6)
                {
                    a[2, j - 2] = Convert.ToInt16(TextBox1[j].Text);
                }
                else
                {
                    a[3, j - 5] = Convert.ToInt16(TextBox1[j].Text);
                }
                //輸出
                for (int i = 0; i < 9; i++)
                {
                    if (i < 3)
                    {
                        Button1[i].Text = Convert.ToString(a[1, i + 1]);
                    }
                    else if (i >= 3 && i < 6)
                    {
                        Button1[i].Text = Convert.ToString(a[2, i - 2]);
                    }
                    else
                    {
                        Button1[i].Text = Convert.ToString(a[3, i - 5]);
                    }
                }
        }
        }
    }
}

沒有留言:

張貼留言