2015年1月15日 星期四

NO.8

VAB的教學與使用,可以利用EXCEL和C++結合!!


Private Sub CommandButton1_Click()
For i = 1 To 9
For j = 1 To 9
    Cells(i, j) = i * j
Next
Next

End Sub

NO.12

這是用堆疊的方法來亂數排列,並用上自動變數,可以用來亂數排列,可以用在撲克牌等遊戲中

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 WindowsFormsApplication12
{
    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)
        {
            // Instantiating all the buttons in the array



            for (int i = 0; i < 9; i++)
            {
                array[i] = i;
            }


            /*
            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 < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    buttons[i, j] = new Button();
                    buttons[i, j].Location = new Point(i * 50, j * 50);
                    buttons[i,j].Text = j.ToString();
                    
                    this.Controls.Add(buttons[i, j]);
                    buttons[i,j].Click += new EventHandler(Button1_Click);
                }
            }

           }

        private void Button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("HELLO");
        }


        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 i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    //buttons[i, j] = new Button();
                    buttons[i, j].Location = new Point(i * 50, j * 50);
                    buttons[i, j].Text = array[i*3+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();

        }
    }

}
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 WindowsFormsApplication12
{
    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)
        {
            // Instantiating all the buttons in the array



            for (int i = 0; i < 9; i++)
            {
                array[i] = i;
            }


            /*
            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 j = 0; j < 3; j++)
            {
                for (int i = 0; i < 3; i++)
                {
                    buttons[i, j] = new Button();
                    buttons[i, j].Location = new Point(j * 50, i * 50);
                    buttons[i,j].Text = (j*3+i).ToString();
                    this.Controls.Add(buttons[i, j]);
                    buttons[i,j].Click += new EventHandler(Button1_Click);
                }
            }

           }

        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 < 3; i++)
            {
                for (int j = 0; j < 3; 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] = new Button();
                    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]);
                }
            }






            //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();

        }
    }

}





2015年1月9日 星期五

先在         public Form1()            建立


第十四週


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
    {
        Button[,] buttons = new Button[10, 10];
        int[] array = new int[10];

        public Form1()       公共Form1中

        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Instantiating all the buttons in the array



            for (int i = 0; i < 9; i++)
            {
                array[i] = i;
            }


            /*
            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 j = 0; j < 3; j++)
            {
                for (int i = 0; i < 3; i++)
                {
                    buttons[i, j] = new Button();
                    buttons[i, j].Location = new Point(j * 50, i * 50);
                    buttons[i, j].Text = (j * 3 + i).ToString();
                    this.Controls.Add(buttons[i, j]);
                    buttons[i, j].Click += new EventHandler(Button1_Click);
                }
            }
        }

        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 < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    if (sender == buttons[i, j])
                    {
                        movement(i, j, buttons[i, j].Text);   移動的盒子
                       
                    }
                }


            }




        }

        private void movement(int i, int j, string s)
        {
            MessageBox.Show("i=" + i + ",j=" + j + " " + s);

            MessageBox.Show("i=" + i  + ",j-1=" + (j-1) + " " + buttons[i, j-1].Text);
            string stmp;

            if (buttons[i, j - 1].Text == "0")
            {
                stmp = buttons[i, j].Text;
                buttons[i, j].Text = buttons[i, j-1].Text;
                buttons[i, j - 1].Text = stmp;
            }

        }


        private void button1_Click_1(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] = new Button();   兩個斜線偵錯
                    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]);
                }
            }






            //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();

        }
   
        }

   
}