2015年11月5日 星期四

簡易計算機
public partial class Form1 : Form
    {
        int a;
        int b1;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            label2.Text = "" + (float.Parse(textBox1.Text) + float.Parse(textBox2.Text));
            //"" 是數值轉換字串的方法,int.Parse是將字串轉為數字的方法
            // 也可以像這樣子打resultVal.Text =  (int.Parse(Val1.Text) + int.Parse(Val2.Text)).ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            label2.Text = "" + (float.Parse(textBox1.Text) - float.Parse(textBox2.Text));
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            label2.Text = "" + (float.Parse(textBox1.Text) * float.Parse(textBox2.Text));
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string b1;
            b1 = textBox2.Text;
            float a = float.Parse(b1);
            if(a==0)
                label2.Text="不能除以0";
            if(a!=0)
                label2.Text = "" + (float.Parse(textBox1.Text) / float.Parse(textBox2.Text));


            
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }
    }

}

2015年10月30日 星期五

推盤

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        Button[,] buttons = new Button[9, 9];     //宣告一個二維陣列  (用來製造4x4按鈕)
        int[] randomize = new int[16];                //宣告一個一維陣列  (用來存取變數的陣列)
        Random rnd = new Random();                //宣告產生亂數
        int x, y;
        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();                  //利用二維陣列產生4x4按鈕
                    buttons[i, j].Location = new Point(i * 100, j * 100);     //改變其按鈕位置
                    buttons[i, j].Size = new Size(100, 100);            //改變按鈕大小    
                    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 < 16; i++)
            {

                randomize[i] = rnd.Next(0, 16);        //將0~15個亂數依序放進一維陣列randomize中

                for (int j = 0; j < i; j++)
                {
                    while (randomize[j] == randomize[i])         // 檢查是否有重複的亂數 如果有就重新產生
                    {
                        j = 0;
                        randomize[i] = rnd.Next(0, 16);
                    }
                }
            }

            for (int i = 0; i < 4; ++i)
            {

                for (int j = 0; j < 4; ++j)
                {
                    buttons[i, j].Text = Convert.ToString(randomize[i * 4 + j]);            //將一維陣列randomize依序放進二維陣列的按鈕上
                    this.Controls.Add(buttons[i, j]);
                    if (buttons[i, j].Text == "0")
                    {
                        buttons[i, j].Text = "";
                        x = i;
                        y = j;
                    }
                }
            }
        }
            private void Button1_Click(object sender, EventArgs e)
            {
            string a;
            if (x > 0)
            {
                if (sender == buttons[x - 1, y])
                {
                    a = buttons[x - 1, y].Text;
                    buttons[x - 1, y].Text = buttons[x, y].Text;
                    buttons[x, y].Text = a;
                    x = x - 1;
                }
            }
            if (x < 3)
            {
                if (sender == buttons[x + 1, y])
                {
                    a = buttons[x + 1, y].Text;
                    buttons[x + 1, y].Text = buttons[x, y].Text;
                    buttons[x, y].Text = a;
                    x = x + 1;
                }
            }
            if (y > 0)
            {
                if (sender == buttons[x, y - 1])
                {
                    a = buttons[x, y - 1].Text;
                    buttons[x, y - 1].Text = buttons[x, y].Text;
                    buttons[x, y].Text = a;
                    y = y - 1;
                }
            }
            if (y < 3)
            {
                if (sender == buttons[x, y + 1])
                {
                    a = buttons[x, y + 1].Text;
                    buttons[x, y + 1].Text = buttons[x, y].Text;
                    buttons[x, y].Text = a;
                    y = y + 1;
                }
        }
    }
}
}

2015年10月23日 星期五

推盤


public partial class Form1 : Form
    {
        int rndmoney1, rndmoney2, rndmoney3, rndmoney4, rndmoney5, rndmoney6, rndmoney7, rndmoney8, rndmoney9;
        public Form1()
        {
            InitializeComponent();
        }

        private void button9_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void button10_Click(object sender, EventArgs e)
        {
            Random rnd = new Random();
            //rndmoney1=rnd.Next(0,10);//button1.Text = "4";
            //rndmoney2=rnd.Next(0,10);//button2.Text = "4";
            //rndmoney3=rnd.Next(0,10);//button3.Text = "4";
            //rndmoney4=rnd.Next(0,10);//button4.Text = "0";
            //rndmoney5=rnd.Next(0,10);//button5.Text = "4";
            //rndmoney6=rnd.Next(0,10);//button6.Text = "9";
            //rndmoney7=rnd.Next(0,10);//button7.Text = "9";
            //rndmoney8=rnd.Next(0,10);//button8.Text = "2";
            //rndmoney9 = rnd.Next(0, 10);//button9.Text = "8";
            button1.Text = rndmoney1.ToString();
            button2.Text = rndmoney2.ToString();
            if (rndmoney1 == rndmoney2)
            {
                MessageBox.Show("Dot Net Perls is awesome");
                rndmoney2 = rnd.Next(0, 9);
                button2.Text = rndmoney2.ToString();
            }
            button3.Text = rndmoney3.ToString();
            if (rndmoney3 == rndmoney2 || rndmoney3==rndmoney1)
            {
                MessageBox.Show("Dot Net Perls is awesome");
                rndmoney3 = rnd.Next(0, 9);
                button3.Text = rndmoney3.ToString();
            }
         
         
            //button4.Text = rndmoney4.ToString();
            //button5.Text = rndmoney5.ToString();
            //button6.Text = rndmoney6.ToString();
            //button7.Text = rndmoney7.ToString();
            //button8.Text = rndmoney8.ToString();
            //button9.Text = rndmoney9.ToString();
            button1.Enabled = false;
            button2.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = false;
            button5.Enabled = false;
            button6.Enabled = false;
            button7.Enabled = false;
            button8.Enabled = false;
            button9.Enabled = false;

2015年10月2日 星期五

int c = 0,d=0,e=0;
        public Form1()
        {   
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            c = c + 1;
            button1.Text = c.ToString();

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            c = c + 1;
            d = c%3;
            button1.Text = c.ToString();
            if (d==1)
            { 
                button1.BackColor = System.Drawing.Color.FromName("Red");
                button2.BackColor = System.Drawing.Color.FromName("White");
                button3.BackColor = System.Drawing.Color.FromName("White");
            }

            else if (d == 0)
            {
                button1.BackColor = System.Drawing.Color.FromName("White");
                button2.BackColor = System.Drawing.Color.FromName("Yellow");
                button3.BackColor = System.Drawing.Color.FromName("White");
            }
            else
            {
                button1.BackColor = System.Drawing.Color.FromName("White");
                button2.BackColor = System.Drawing.Color.FromName("White");
                button3.BackColor = System.Drawing.Color.FromName("Green");
            }
            

        }