2010年10月29日 星期五

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[] Buttons;
        System.Windows.Forms.TextBox[] textboxs;
        int[,] a = new int[4, 4];
        int bno;
        Form2 newform;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Buttons = new System.Windows.Forms.Button[9];
            textboxs = new System.Windows.Forms.TextBox[9];
            for (int i = 0; i < 9; ++i)
            {
                Buttons[i] = new Button();

                Buttons[i].Click += new System.EventHandler(Buttons_Click);
                this.Controls.Add(Buttons[i]);
                if (i <= 2)
                    Buttons[i].Location = new System.Drawing.Point(10 + i * 80, 10 + 100);
                else if (i > 2 && i <= 5)
                    Buttons[i].Location = new System.Drawing.Point(10 + (i - 3) * 80, 40 + 100);
                else if (i > 5 && i <= 9)
                    Buttons[i].Location = new System.Drawing.Point(10 + (i - 6) * 80, 70 + 100);
            }

           

            for (int j = 0; j < 9; j++)
            {
                textboxs[j] = new TextBox();
                this.Controls.Add(textboxs[j]);
                if (j <= 2)
                    textboxs[j].Location = new System.Drawing.Point(10 + j * 110, 10);
                else if (j > 2 && j <= 5)
                    textboxs[j].Location = new System.Drawing.Point(10 + (j - 3) * 110, 40);
                else if (j > 5 && j <= 9)
                    textboxs[j].Location = new System.Drawing.Point(10 + (j - 6) * 110, 70);

            }
            textboxs[0].Text = "7";
            textboxs[1].Text = "2";
            textboxs[2].Text = "4";
            textboxs[3].Text = "5";
            textboxs[4].Text = "0";
            textboxs[5].Text = "6";
            textboxs[6].Text = "8";
            textboxs[7].Text = "3";
            textboxs[8].Text = "1";


        }
        public void Buttons_Click(object sender, EventArgs e)
        {
            // System.Windows.Forms.MessageBox.Show("You have clicked button " +
            //   ((System.Windows.Forms.Button)sender).Tag.ToString());
           String tnum = sender.ToString();
            int tlen = tnum.Length;
            String no = tnum.Substring(tlen - 1);
            for (int j = 0; j < 9; j++)
            {
                if (Buttons[j].Text == no)
                    bno = j;                 
            }
            switch (bno)
            {
                case 0:
                    if (Buttons[1].Text == "0")
                    {
                        String temp;
                        temp = Buttons[1].Text;
                        Buttons[1].Text = Buttons[0].Text;
                        Buttons[0].Text = temp;
                    }
                    if (Buttons[3].Text == "0")
                    {
                        String temp;
                        temp = Buttons[3].Text;
                        Buttons[3].Text = Buttons[0].Text;
                        Buttons[0].Text = temp;
                    }
                   
                    break;
                case 1:
                    if (Buttons[0].Text == "0")
                    {
                        String temp;
                        temp = Buttons[0].Text;
                        Buttons[0].Text = Buttons[1].Text;
                        Buttons[1].Text = temp;
                    }
                    if (Buttons[2].Text == "0")
                    {
                        String temp;
                        temp = Buttons[2].Text;
                        Buttons[2].Text = Buttons[1].Text;
                        Buttons[1].Text = temp;
                    }
                    if (Buttons[4].Text == "0")
                    {
                        String temp;
                        temp = Buttons[4].Text;
                        Buttons[4].Text = Buttons[1].Text;
                        Buttons[1].Text = temp;
                    }
                    break;
                case 2:
                    if (Buttons[1].Text == "0")
                    {
                        String temp;
                        temp = Buttons[1].Text;
                        Buttons[1].Text = Buttons[2].Text;
                        Buttons[2].Text = temp;
                    }
                    if (Buttons[5].Text == "0")
                    {
                        String temp;
                        temp = Buttons[5].Text;
                        Buttons[5].Text = Buttons[2].Text;
                        Buttons[2].Text = temp;
                    }
                    break;
                case 3:
                    if (Buttons[4].Text == "0")
                    {
                        String temp;
                        temp = Buttons[4].Text;
                        Buttons[4].Text = Buttons[3].Text;
                        Buttons[3].Text = temp;
                    }
                    if (Buttons[0].Text == "0")
                    {
                        String temp;
                        temp = Buttons[0].Text;
                        Buttons[0].Text = Buttons[3].Text;
                        Buttons[3].Text = temp;
                    }
                    if (Buttons[6].Text == "0")
                    {
                        String temp;
                        temp = Buttons[6].Text;
                        Buttons[6].Text = Buttons[3].Text;
                        Buttons[3].Text = temp;
                    }
                    break;
                case 4:
                    if (Buttons[1].Text == "0")
                    {
                        String temp;
                        temp = Buttons[1].Text;
                        Buttons[1].Text = Buttons[4].Text;
                        Buttons[4].Text = temp;
                    }
                    if (Buttons[3].Text == "0")
                    {
                        String temp;
                        temp = Buttons[3].Text;
                        Buttons[3].Text = Buttons[4].Text;
                        Buttons[4].Text = temp;
                    }
                    if (Buttons[5].Text == "0")
                    {
                        String temp;
                        temp = Buttons[5].Text;
                        Buttons[5].Text = Buttons[4].Text;
                        Buttons[4].Text = temp;
                    }
                    if (Buttons[7].Text == "0")
                    {
                        String temp;
                        temp = Buttons[7].Text;
                        Buttons[7].Text = Buttons[4].Text;
                        Buttons[4].Text = temp;
                    }
                    break;
                case 5:
                    if (Buttons[2].Text == "0")
                    {
                        String temp;
                        temp = Buttons[2].Text;
                        Buttons[2].Text = Buttons[5].Text;
                        Buttons[5].Text = temp;
                    }
                    if (Buttons[4].Text == "0")
                    {
                        String temp;
                        temp = Buttons[4].Text;
                        Buttons[4].Text = Buttons[5].Text;
                        Buttons[5].Text = temp;
                    }
                    if (Buttons[8].Text == "0")
                    {
                        String temp;
                        temp = Buttons[8].Text;
                        Buttons[8].Text = Buttons[5].Text;
                        Buttons[5].Text = temp;
                    }
                    break;
                case 6:
                    if (Buttons[3].Text == "0")
                    {
                        String temp;
                        temp = Buttons[3].Text;
                        Buttons[3].Text = Buttons[6].Text;
                        Buttons[6].Text = temp;
                    }
                    if (Buttons[7].Text == "0")
                    {
                        String temp;
                        temp = Buttons[7].Text;
                        Buttons[7].Text = Buttons[6].Text;
                        Buttons[6].Text = temp;
                    }
                    break;
                case 7:
                    if (Buttons[4].Text == "0")
                    {
                        String temp;
                        temp = Buttons[4].Text;
                        Buttons[4].Text = Buttons[7].Text;
                        Buttons[7].Text = temp;
                    }
                    if (Buttons[6].Text == "0")
                    {
                        String temp;
                        temp = Buttons[6].Text;
                        Buttons[6].Text = Buttons[7].Text;
                        Buttons[7].Text = temp;
                    }
                    if (Buttons[8].Text == "0")
                    {
                        String temp;
                        temp = Buttons[8].Text;
                        Buttons[8].Text = Buttons[7].Text;
                        Buttons[7].Text = temp;
                    }
                    break;
                case 8:
                    if (Buttons[5].Text == "0")
                    {
                        String temp;
                        temp = Buttons[5].Text;
                        Buttons[5].Text = Buttons[8].Text;
                        Buttons[8].Text = temp;
                    }
                    if (Buttons[7].Text == "0")
                    {
                        String temp;
                        temp = Buttons[7].Text;
                        Buttons[7].Text = Buttons[8].Text;
                        Buttons[8].Text = temp;
                    }
                    break;
            }

        }

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



        }

        private void button2_Click(object sender, EventArgs e)
        {
            newform = new Form2();
            newform.Show();
        }
    }
}

順序 5-7-2-5-6-1-3--8-7-6-1-3-8-7-6-1-3-4-5--2-1-3-4-5-2-1
共26步

沒有留言:

張貼留言