2015年1月8日 星期四

梁凱恩立志影片心得

罪惡的六個字,不知道不相信,不知道還有救不相信就沒救了可以埋一埋,獎詩婷醒我們時時刻刻清楚自己的夢想,脫胎換骨改頭換面
影片連結 : https://www.youtube.com/watch?v=Ynrj6eUEqAQ

產生button


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 WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
button1.Location = new Point(100, 100);
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show ("hello!");
}
}
}

紅綠燈

目標 : 建立紅綠燈燈號轉換功能,藉由按鍵觸發開關
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 WindowsFormsApplication2
{
public partial class Form1 : Form
{
int c, r;

public Form1()
{
InitializeComponent();
c = 0;
}

private void button4_Click(object sender, EventArgs e)
{
c = c + 1;
r = c % 3;

if (r == 0)
{
button1.BackColor = Color.Red;
button2.BackColor = Color.White;
button3.BackColor = Color.White;
}
else if (r == 1)
{
button1.BackColor = Color.White;
button2.BackColor = Color.Yellow;
button3.BackColor = Color.White;
}
else
{
button1.BackColor = Color.White;
button2.BackColor = Color.White;
button3.BackColor = Color.Blue;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
c = c + 1;
r = c % 3;

if (r == 0)
{
button1.BackColor = Color.Red;
button2.BackColor = Color.White;
button3.BackColor = Color.White;
}
else if (r == 1)
{
button1.BackColor = Color.White;
button2.BackColor = Color.Yellow;
button3.BackColor = Color.White;
}
else
{
button1.BackColor = Color.White;
button2.BackColor = Color.White;
button3.BackColor = Color.Blue;
}
}
}
}

丟骰子兩匹馬賽跑

目標 : 做出一個隨機骰子,其結果將決定Player1、Player2前進步數,順序輪流且至設定終點時跳出勝利宣告。


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;
using System.Threading;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int ssum;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            int d1, d2,dsum;
            Random irand=new Random();
            d1=irand.Next(1,7);
            d2=irand.Next(1,7);
            label1.Text=Convert.ToString(d1);
            label2.Text=Convert.ToString(d2);
            dsum=d1+d2;
            for (int i = 1; i <= d1 + d2; i++)
            {
                Thread.Sleep(100);
                Application.DoEvents();
                button2.Left = i+ssum;
            }
            ssum=ssum+dsum;
            textBox1.Text = Convert.ToString(ssum);
           
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

    }
}

圈叉遊戲


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 WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        int c=0;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            button1.Text = "";
            button2.Text = "";
            button3.Text = "";
            button4.Text = "";
            button5.Text = "";
            button6.Text = "";
            button7.Text = "";
            button8.Text = "";
            button9.Text = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;
            if (r == 0)
            {
                button1.Text = "O";
            }
            else
            {
                button1.Text = "X";
            }
            button1.Enabled = false;
            if ((button1.Text == button2.Text && button2.Text == button3.Text) ||
               (button1.Text == button5.Text && button5.Text == button9.Text) ||
               (button1.Text == button4.Text && button4.Text == button7.Text))
            {
                MessageBox.Show("贏了");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;           
            if (r == 1)
            { button2.Text = "O"; }
            else
            { button2.Text = "X"; }
            button2.Enabled = false;
            if ((button2.Text == button1.Text && button1.Text == button3.Text) ||
            (button2.Text == button5.Text && button5.Text == button8.Text))

            {
                MessageBox.Show("贏了");
            }
        }
       

        private void button3_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;           
            if (r == 1)
            { button3.Text = "O"; }
            else
            { button3.Text = "X"; }
            button3.Enabled = false;
            if ((button3.Text == button2.Text && button2.Text == button1.Text) ||
            (button3.Text == button6.Text && button6.Text == button9.Text) ||
            (button3.Text == button5.Text && button5.Text == button7.Text))
            {
                MessageBox.Show("贏了");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;           
            if (r == 1)
            { button4.Text = "O"; }
            else
            { button4.Text = "X"; }
            button4.Enabled = false;
            if ((button4.Text == button5.Text && button5.Text == button6.Text) ||
            (button4.Text == button1.Text && button1.Text == button7.Text))

            {
                MessageBox.Show("贏了");
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;           
            if (r == 1)
            { button5.Text = "O"; }
            else
            { button5.Text = "X"; }
            button5.Enabled = false;
            if ((button5.Text == button4.Text && button4.Text == button6.Text) ||
            (button5.Text == button2.Text && button2.Text == button8.Text) ||
            (button5.Text == button1.Text && button1.Text == button9.Text) ||
            (button5.Text == button3.Text && button3.Text == button7.Text))
            {
                MessageBox.Show("贏了");
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;           
            if (r == 1)
            { button6.Text = "O"; }
            else
            { button6.Text = "X"; }
            button6.Enabled = false;
            if ((button3.Text == button2.Text && button2.Text == button1.Text) ||
            (button3.Text == button6.Text && button6.Text == button9.Text) ||
            (button3.Text == button5.Text && button5.Text == button7.Text))
            {
                MessageBox.Show("贏了");
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;           
            if (r == 1)
            { button7.Text = "O"; }
            else
            { button7.Text = "X"; }
            button7.Enabled = false;
            if ((button7.Text == button1.Text && button1.Text == button4.Text) ||
            (button7.Text == button8.Text && button8.Text == button9.Text) ||
            (button7.Text == button5.Text && button5.Text == button3.Text))
            {
                MessageBox.Show("贏了");
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;           
            if (r == 1)
            { button8.Text = "O"; }
            else
            { button8.Text = "X"; }
            button8.Enabled = false;
            if ((button8.Text == button2.Text && button2.Text == button5.Text) ||
            (button8.Text == button7.Text && button7.Text == button9.Text))
            {
                MessageBox.Show("贏了");
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            int r;
            c = c + 1;
            r = c % 2;           
            if (r == 1)
            { button9.Text = "O"; }
            else
            { button9.Text = "X"; }
            button9.Enabled = false;
            if ((button9.Text == button3.Text && button3.Text == button6.Text) ||
            (button9.Text == button8.Text && button8.Text == button7.Text) ||
            (button9.Text == button5.Text && button5.Text == button1.Text))
            {
                MessageBox.Show("贏了");
            }           
        }
        private void judge()
        {
            MessageBox.Show("贏了");
        }
    }
}

數字陣列

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.Button[] Buttons;
        public Form1()
        {
            InitializeComponent();
           
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            mybuttons();
            //Button1 = new System.Windows.Forms.Button();
            //Button1.Location = new Point(100, 100);
            //Button1.Size = new Size(100, 100);
            //Button1.Click += new System.EventHandler(Button1_Click);
            for (int i = 0; i < Buttons.Length; i++)
            {
                int index = i;
                Buttons[i].Click += (sender1, ex) => this.Display(index + 1);
                this.Controls.Add(Button1);
            }
        }
        public void Display(int i)
        {
            MessageBox.Show("Button No " + i);
        }
        private void Button1_Click(object sender, EventArgs e)
        {
           
            MessageBox.Show("zzzzzzzzzzz");
        }
        private void mybuttons()
        {

            int width, height;
            //Button1 = new System.Windows.Forms.Button();
            //Button1.Location = new Point(200, 200);
            //Button1.Size = new Size(200, 200);
            //this.Controls.Add(Button1);
            width = 50;
            height = 50;
            Buttons = new System.Windows.Forms.Button[9];

            for (int i = 0; i < 9; ++i)
            {
                Buttons[i] = new Button();
                this.Controls.Add(Buttons[i]);
                Buttons[i].Size = new Size(width, height);
                Buttons[i].Text = i.ToString();
                Buttons[i].BackColor = Color.Red;
                if (i <= 2)
                    Buttons[i].Location = new System.Drawing.Point(100 + i * 50, 10);
                else if (i > 2 && i <= 5)
                    Buttons[i].Location = new System.Drawing.Point(100 + (i - 3) * 50, 60);
                else if (i > 5 && i <= 9)
                    Buttons[i].Location = new System.Drawing.Point(100 + (i - 6) * 50, 110);

            }
        }
    }
}

圈叉遊戲陣列

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.Button[] Buttons;
        Button[,] buttons = new Button[4, 4];
        int c = 0;
        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {           
            //Button1 = new System.Windows.Forms.Button();
            //Button1.Location = new Point(100, 100);
            //Button1.Size = new Size(100, 100);
            //Button1.Click += new System.EventHandler(Button1_Click);
           
            //Buttons[,]=new System.Windows.Forms.Button[];        
            mouse_Click();
        }       
        private void mouse_Click()
        {
            for (int i = 1; i < 4; i++)
            {
                for (int j = 1; j < 4; j++)
                {
                    buttons[i, j] = new Button();
                    buttons[i, j].Location = new Point(100 + i * 50, j * 50);
                    buttons[i, j].Size = new Size(50, 50);
                    //buttons[i, j].Text = (i*j).ToString();
                    this.Controls.Add(buttons[i, j]);

                    int r;
                    c = c + 1;
                    r = c % 2;
                    buttons[i, j].Text = Convert.ToString(r);

                    if (r == 0)
                    {
                        buttons[i, j].Text = "O";
                    }
                    else
                    {
                        buttons[i, j].Text = "X";
                    }
                }
            }
        }
       
    }
}

99乘法表

使用EXCEL內建VBA語言寫出陣列格式
excel 99乘法表

Private Sub CommandButton1_Click()
Application.ActiveSheet.Cells(1, 1).Value = "Excel VBA 99乘法表"
For C = 1 To 9
Application.Cells(1, C).ColumnWidth = 3
Next
For I = 1 To 9
For J = 1 To 9
Application.ActiveSheet.Cells(I + 1, J).Value = I * J
Next
Next
End Sub
Private Sub UserForm_Click()
End Sub

c#99格

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 WindowsFormsApplication6
{
public partial class Form1 : Form
{
Button[,] buttons = new Button[10, 10];
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
for (int i = 1; i < 10; i++)
{
for (int j = 1; j < 10; j++)
{
buttons[i, j] = new Button();
buttons[i, j].Location = new Point(i * 30, j * 30);
buttons[i, j].Size = new Size(30, 30);
buttons[i, j].Text = Convert.ToString(i * j);

this.Controls.Add(buttons[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 WindowsFormsApplication4
{
public partial class Form1 : Form
{
Button[,] buttons = new Button[4, 4];
Random irand = new Random();
int rd = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
buttons[i, j] = new Button();
buttons[i, j].Location = new Point(80 + i * 50, 80 + j * 50);
buttons[i, j].Size = new Size(50, 50);
buttons[i, j].Text = Convert.ToString(i * j);
rd = irand.Next(0, 16);
buttons[i, j].Text = Convert.ToString(rd);
this.Controls.Add(buttons[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 WindowsFormsApplication7
{
    public partial class Form1 : Form
    {
        Button[,] buttons = new Button[5,5];
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int x;
            int width, height;
            int[] myarray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
            // Instantiating all the buttons in the array
            for (int j = 1; j < 5; j++)
            {
                for (int i = 1; i < 5; i++)
                {
                    width = this.Size.Width;
                    height = this.Size.Height;
                    buttons[i, j] = new Button();
                    buttons[i, j].Location = new Point(i * 50, j * 50);
                    //x = (i + 1) * (j + 1);
                    x = myarray[(j - 1) * 4 + i - 1];

                    width = 50;
                    height = 50;
                    buttons[i, j].Size = new Size(width, height);
                    buttons[i, j].Text = x.ToString();
                    this.Controls.Add(buttons[i, j]);
                }
            }
        }
    }
}

4X4陣列亂數

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
    {
        public System.Windows.Forms.Button Button1;
        public System.Windows.Forms.Button Button2;
        Random rand = new Random();
        Button[,] buttons = new Button[5, 5];
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Button1 = new System.Windows.Forms.Button();
            Button1.Location = new Point(300, 30);
            Button1.Size = new Size(100, 30);
            Button1.Click += new EventHandler(Button1_Click);
            Button1.Text = "change";
            this.Controls.Add(Button1);
            /*Button2 = new System.Windows.Forms.Button();
            Button2.Location = new Point(200, 50);
            Button2.Size = new Size(100, 30);
            Button2.Text = "Change";
            Button2.Click += new EventHandler(Button2_Click);
            this.Controls.Add(Button2);
             */
            for (int j = 1; j < 5; j++)
            {
                for (int i = 1; i < 5; i++)
                {
                    buttons[i, j] = new Button();
                    buttons[i, j].Location = new Point(  i * 50,  j * 50);
                    buttons[i, j].Size = new Size(50, 50);
                    this.Controls.Add(buttons[i, j]);
                }
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            int x, y, a;
            int[] myarray = { 0, 1, 2, 3, 4, 5, 6, 7, 8,9,10,11,12,13,14,15 };
            for (int i = 15; i > 1; i--)
            {
                x = rand.Next(0, i);
                y = myarray[x];
                myarray[x] = myarray[i];
                myarray[i] = y;
            }
            for (int j = 1; j < 5; j++)
            {
                for (int i = 1; i < 5; i++)
                {
                    a = myarray[(j - 1) * 4 + (i - 1)];
                    buttons[i, j].Text = Convert.ToString(a);
                    this.Controls.Add(buttons[i, j]);
                }
            }
        }
        protected void Button2_Click(object sender, EventArgs e)
        {

        }
    }
}