2015年12月24日 星期四

20151225_Database6_FinalExam

程式設計工藝大師


Database6_FinalExam


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.Data.OleDb;
using System.IO;

namespace WindowsFormsApplication7
{
    public partial class Form1 : Form
    {
        private OleDbConnection connection = new OleDbConnection();

        OleDbDataAdapter dAdapter;
        OleDbCommandBuilder cBuilder;
        DataTable dTable = new DataTable();
        BindingSource bSource;

        private string ID;

        public Form1()
        {
           
            InitializeComponent();
            connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\student\Desktop\WindowsFormsApplication7\WindowsFormsApplication7\WindowsFormsApplication7\bin\Debug\test1.mdb");


        }

   

        private void Form1_Load(object sender, EventArgs e)
        {
       

            dAdapter = new OleDbDataAdapter("select * from person where 識別碼  ", connection);

            cBuilder = new OleDbCommandBuilder(dAdapter);
            dAdapter.Fill(dTable);

            bSource = new BindingSource();
            bSource.DataSource = dTable;

            dataGridView1.DataSource = bSource;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            connection.Open();

            OleDbCommand command2 = new OleDbCommand();
            command2.Connection = connection;

            command2.CommandText = "insert into person (name,tel,stu_no,sex) values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')";

            command2.ExecuteNonQuery();

            dAdapter.Fill(dTable);

            connection.Close();
             
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {

                try
                {
                    var Value = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
                    Value = dataGridView1.Rows[e.RowIndex].Cells["name"].Value;

                    connection.Open();
                    OleDbCommand command = new OleDbCommand();
                    command.Connection = connection;

                    string query = "select* from person where name='" + Value.ToString() + "'";
                    command.CommandText = query;


                    OleDbDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {

                        ID = reader["識別碼"].ToString();
                        textBox1.Text = reader["name"].ToString();
                        textBox2.Text = reader["tel"].ToString();
                        textBox3.Text = reader["stu_no"].ToString();
                        textBox4.Text = reader["sex"].ToString();
                       

                    }
                    connection.Close();
                }

                catch (Exception ex)
                {
                    MessageBox.Show("ERROR" + ex);
                }
            }

            else if (e.ColumnIndex == 1)
            {
                if (MessageBox.Show("確定刪除此筆資料?", "刪除資料", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    try
                    {
                        var Value = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
                        Value = dataGridView1.Rows[e.RowIndex].Cells["name"].Value;
                       
                        connection.Open();
                        OleDbCommand command = new OleDbCommand();
                        command.Connection = connection;



                        command.CommandText = "delete from person WHERE name = '" + Value.ToString() + "'";

                        command.ExecuteNonQuery();
                        dTable.Clear();
                        dAdapter.Fill(dTable);

                        connection.Close();
                        MessageBox.Show("刪除成功");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("ERROR" + ex);
                    }

                }
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
                         dTable.Clear();

                        connection.Open();
                        OleDbCommand command = new OleDbCommand();
                        command.Connection = connection;




                        command.CommandText = "UPDATE person SET name = '" + textBox1.Text + "'WHERE 識別碼 = " + ID ;

                      command.ExecuteNonQuery();

                      dAdapter.Fill(dTable);
        }
    }
}

2015年12月17日 星期四

20151218_剪刀石頭布

程式設計工藝大師


剪刀石頭布



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
    {
        Random rand = new Random();
        int r;
        string winner, time;

        public Form1()
        {
            InitializeComponent();          
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: 這行程式碼會將資料載入 'testDataSet.data' 資料表。您可以視需要進行移動或移除。
            this.dataTableAdapter.Fill(this.testDataSet.data);
         
        }

        private void button1_Click(object sender, EventArgs e)
        {
            r = rand.Next(1, 4);
            if (r == 1)
            {
                winner = "Draw";
                richTextBox1.Text += "Draw\n";
                label6.Text = "剪刀";
            }
            if (r == 2)
            {
                winner = "Computer";
                richTextBox1.Text += "Computer\n";
                label6.Text = "石頭";
            }
            if (r == 3)
            {
                winner = "Player";
                richTextBox1.Text += "Playler\n";
                label6.Text = "布";
            }
            label5.Text = "剪刀";
            time = DateTime.Now.ToLongTimeString();
            richTextBox2.Text += DateTime.Now.ToLongTimeString()+"\n";
            dataTableAdapter.Insert(winner, time);
       
        }

        private void button2_Click(object sender, EventArgs e)
        {
            r = rand.Next(1, 4);
            if (r == 1)
            {
                winner = "Player";
                richTextBox1.Text += "Player\n";
                label6.Text = "剪刀";
            }
            if (r == 2)
            {
                winner = "Draw";
                richTextBox1.Text += "Draw\n";
                label6.Text = "石頭";
            }
            if (r == 3)
            {
                winner = "Computer";
                richTextBox1.Text += "Computer\n";
                label6.Text = "布";
            }
            label5.Text = "石頭";
            time = DateTime.Now.ToLongTimeString();
            richTextBox2.Text += DateTime.Now.ToLongTimeString() + "\n";
            dataTableAdapter.Insert(winner, time);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            r = rand.Next(1, 4);
            if (r == 1)
            {
                winner = "Computer";
                richTextBox1.Text += "Computer\n";
                label6.Text = "剪刀";
            }
            if (r == 2)
            {
                winner = "Player";
                richTextBox1.Text += "Player\n";
                label6.Text = "石頭";
            }
            if (r == 3)
            {
                winner = "Draw";
                richTextBox1.Text += "Draw\n";
                label6.Text = "布";
            }
            label5.Text = "布";
            time = DateTime.Now.ToLongTimeString();
            richTextBox2.Text += DateTime.Now.ToLongTimeString() + "\n";
            dataTableAdapter.Insert(winner, time);
        }
    }
}





20151218_Database5

程式設計工藝大師

Database5


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            textBox4.DataBindings.Add("Text", bindingSource1, "stu_no", true);
            textBox1.DataBindings.Add("Text", bindingSource1, "name", true);
            textBox2.DataBindings.Add("Text", bindingSource1, "sex", true);
            textBox3.DataBindings.Add("Text", bindingSource1, "tel", true);    
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: 這行程式碼會將資料載入 'db1DataSet.DataTable1' 資料表。您可以視需要進行移動或移除。
            this.dataTable1TableAdapter.Fill(this.db1DataSet.DataTable1);
            // TODO: 這行程式碼會將資料載入 'db1DataSet.record' 資料表。您可以視需要進行移動或移除。
            this.recordTableAdapter.Fill(this.db1DataSet.record);
            // TODO: 這行程式碼會將資料載入 'db1DataSet.person' 資料表。您可以視需要進行移動或移除。
            this.personTableAdapter.Fill(this.db1DataSet.person);                
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bindingSource1.MoveNext();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            bindingSource1.MovePrevious();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            bindingSource1.MoveFirst();
        }

        private void button4_Click(object sender, EventArgs e)
        {          
            try
            {
                bindingSource1.AddNew();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void button5_Click(object sender, EventArgs e)
        {
              try
            {
                bindingSource1.EndEdit();
                personTableAdapter.Update(db1DataSet.person);

                MessageBox.Show("資料更新成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            //personTableAdapter.Update(db1DataSet.person);                      
        }

    }
}

2015年12月11日 星期五

20151211_Database4

程式設計工藝大師

DataBase4


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int[] a = new int[10];
        int k = 0,i,b;
        float sum = 0;
        public Form1()
        {
            InitializeComponent();
            textBox1.DataBindings.Add("Text", bindingSource3, "name", true);
            textBox2.DataBindings.Add("Text", bindingSource3, "math", true);
            a[0] = 0;
            a[1] = 0;
            a[2] = 0;
        }

        private void bindingSource1_CurrentChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: 這行程式碼會將資料載入 'db1DataSet.DataTable1' 資料表。您可以視需要進行移動或移除。
            this.dataTable1TableAdapter.Fill(this.db1DataSet.DataTable1);
            // TODO: 這行程式碼會將資料載入 'db1DataSet.record' 資料表。您可以視需要進行移動或移除。
            this.recordTableAdapter.Fill(this.db1DataSet.record);
            // TODO: 這行程式碼會將資料載入 'db1DataSet.person' 資料表。您可以視需要進行移動或移除。
            this.personTableAdapter.Fill(this.db1DataSet.person);

        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

     

        private void button2_Click_1(object sender, EventArgs e)
        {
            bindingSource3.MoveNext();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bindingSource3.MoveFirst();
        }

       

        private void button3_Click(object sender, EventArgs e)
        {
            b= bindingSource3.Count;
            for (i = 0; i < b; i++)
            {
                a[i] = int.Parse(textBox2.Text);
                bindingSource3.MoveNext();
            }
            for (i = 0; i < b; i++)
            {
                sum += a[i];
            }
            textBox3.Text = (sum/b).ToString();
            button3.Enabled = false;
     
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
         
        }
    }
}



2015年12月3日 星期四

20151204_DataBase3

程式設計工藝大師

DataBase3


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: 這行程式碼會將資料載入 'db1DataSet1.DataTable2' 資料表。您可以視需要進行移動或移除。
            this.dataTable2TableAdapter.Fill(this.db1DataSet1.DataTable2);
            // TODO: 這行程式碼會將資料載入 'db1DataSet.DataTable1' 資料表。您可以視需要進行移動或移除。
            this.dataTable1TableAdapter.Fill(this.db1DataSet.DataTable1);
            // TODO: 這行程式碼會將資料載入 'db1DataSet.record' 資料表。您可以視需要進行移動或移除。
            this.recordTableAdapter.Fill(this.db1DataSet.record);
            // TODO: 這行程式碼會將資料載入 'db1DataSet.person' 資料表。您可以視需要進行移動或移除。
            this.personTableAdapter.Fill(this.db1DataSet.person);

        }

        private void dataGridView4_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView3_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            // If you are not at the end of the list, move to the next item
            // in the BindingSource.
            if (bindingSource3.Position + 1 < bindingSource3.Count)
                bindingSource3.MoveNext();

            // Otherwise, move back to the first item.
            else
                bindingSource3.MoveFirst();

            // Force the form to repaint.
            this.Invalidate();
            //textBox1.DataBindings.Add("Text", bindingSource3, "name");
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            // If you are not at the end of the list, move to the next item
            // in the BindingSource.
            if (bindingSource3.Position + 1 > bindingSource3.Count)
                bindingSource3.MoveLast();

            // Otherwise, move back to the first item.
            else
                bindingSource3.MovePrevious();

            // Force the form to repaint.
            this.Invalidate();
            //textBox1.DataBindings.Add("Text", bindingSource3, "name");
           

        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.DataBindings.Add("Text", bindingSource3, "name");
        }
    }
}

2015年11月5日 星期四

20151106_計算機

程式設計工藝大師

計算機


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            label2.Text = Convert.ToString(double.Parse(textBox1.Text) + double.Parse(textBox2.Text));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            label2.Text = Convert.ToString(double.Parse(textBox1.Text) - double.Parse(textBox2.Text));
        }

        private void button3_Click(object sender, EventArgs e)
        {
            label2.Text = Convert.ToString(double.Parse(textBox1.Text) * double.Parse(textBox2.Text));
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (double.Parse(textBox2.Text) == 0)
            {
                label2.Text = " ";
                MessageBox.Show("cant 0");
                return;
            }
            label2.Text = Convert.ToString(double.Parse(textBox1.Text) / double.Parse(textBox2.Text));
        }
    }
}




2015年10月31日 星期六

20151030_推盤2

程式設計工藝大師

推盤2


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 System.Windows.Forms.Button[5, 5];
        int i, j;
        Random rnd = new Random();  

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for (i = 1; i <= 4; i++)
            {
                for (j = 1; j <= 4; j++)
                {
                    Buttons[i, j] = new Button();
                    Buttons[i, j].Size = new Size(50, 50);
                    Buttons[i, j].Location = new Point(i * 50, j * 50);
                    this.Controls.Add(Buttons[i, j]);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int product,k=0;
            product = 0;
            int[] A=new int[16];
            for (int i = 0; i < 16; i++)
            {

                A[i] = rnd.Next(0, 16);      

                for (int j = 0; j < i; j++)
                {
                    while (A[j] == A[i])        
                    {
                        j = 0;
                        A[i] = rnd.Next(0, 16);
                    }
                }
            }
            for (i = 1; i <= 4; i++)
            {
                for (j = 1; j <= 4; j++)
                {
                   
                    //product++;
                    Buttons[i, j].Text = A[k++].ToString();
                }
            }
        }
    }
}






2015年10月23日 星期五

20151023_推盤

程式設計工藝大師

推盤


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int rand1;
        int rand2;
        int rand3;
        int rand4;
        int rand5;
        int rand6;
        int rand7;
        int rand8;
        int rand9;
        public Form1()
        {
            InitializeComponent();
        }

        private void button7_Click(object sender, EventArgs e)
        {

        }

        private void button10_Click(object sender, EventArgs e)
        {
            Random rand = new Random();
            rand1 = rand.Next(0,9);
            rand2 = rand.Next(0, 9);
            rand3 = rand.Next(0, 9);
            rand4 = rand.Next(0, 9);
            rand5 = rand.Next(0, 9);
            rand6 = rand.Next(0, 9);
            rand7 = rand.Next(0, 9);
            rand8 = rand.Next(0, 9);
            rand9 = rand.Next(0, 9);
            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;

            do
            {
                rand1 = rand.Next(0,9);
                rand2 = rand.Next(0, 9);
                rand3 = rand.Next(0, 9);
                rand4 = rand.Next(0, 9);
                rand5 = rand.Next(0, 9);
                rand6 = rand.Next(0, 9);
                rand7 = rand.Next(0, 9);
                rand8 = rand.Next(0, 9);
                rand9 = rand.Next(0, 9);

                button1.Text=rand1.ToString();
                button2.Text=rand2.ToString();
                button3.Text=rand3.ToString();
                button4.Text=rand4.ToString();
                button5.Text=rand5.ToString();
                button6.Text=rand6.ToString();
                button7.Text=rand7.ToString();
                button8.Text=rand8.ToString();
                button9.Text=rand9.ToString();

            }while((rand1==rand2)||
            (rand1==rand3)||(rand2==rand3)||
            (rand1==rand4)||(rand2==rand4)||(rand3==rand4)||
            (rand1==rand5)||(rand2==rand5)||(rand3==rand5)||(rand4==rand5)||
            (rand1==rand6)||(rand2==rand6)||(rand3==rand6)||(rand4==rand6)||(rand5==rand6)||
            (rand1==rand7)||(rand2==rand7)||(rand3==rand7)||(rand4==rand7)||(rand5==rand7)||(rand6==rand7)||
            (rand1 == rand8) || (rand2 == rand8) || (rand3 == rand8) || (rand4 == rand8) || (rand5 == rand8) || (rand6 == rand8) || (rand7 == rand8) ||
            (rand1 == rand9) || (rand2 == rand9) || (rand3 == rand9) || (rand4 == rand9) || (rand5 == rand9) || (rand6 == rand9) || (rand7 == rand9) || (rand8 == rand9));
        }
    }
}

2015年10月16日 星期五

20151016_拉霸機

程式設計工藝大師


拉霸機


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int a1 = 0, d1, randnum1 = 10, randnum2 = 10, randnum3 = 10;
        int a2 = 0, d2, a3 = 0, d3;
        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            a1++;
            d1 = a1 % 10;
            if (a1 >= randnum1)
            {
                timer1.Enabled = false;
            }
            button1.Text = d1.ToString();
            if ((randnum1 == randnum2) && (randnum2 == randnum3))
                MessageBox.Show("Win");
        }

        private void button1_Click(object sender, EventArgs e)
        {
       

        }

        private void button4_Click(object sender, EventArgs e)
        {
            a1 = 0;
            a2 = 0;
            a3 = 0;
            timer1.Enabled = true;
            timer2.Enabled = true;
            timer3.Enabled = true;
            Random rnd = new Random();
            randnum1 = rnd.Next(1, 10);
            randnum2 = rnd.Next(1, 10);
            randnum3 = rnd.Next(1, 10);
            //randnum2 = randnum1 + randnum2;
            //randnum3 = randnum1 + randnum2 + randnum3;
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            a2++;
            d2 = a2 % 10;
            if (a2 >= randnum2)
            {
                timer2.Enabled = false;
            }
            button2.Text = d2.ToString();
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            a3++;
            d3 = a3 % 10;
            if (a3 >= randnum3)
            {
                timer3.Enabled = false;
            }
            button3.Text = d3.ToString();
        }

        private void timer4_Tick(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
        }
    }
}



2015年10月1日 星期四

20151002_紅綠燈

程式設計工藝大師


=======================================

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int c = 0, d = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
                   
        }

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

            //button1.Text = c.ToString();
         
        }
    }
}