2015年12月25日 星期五

資料庫(刪除修改)

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.ACE.OLEDB.12.0;Data Source=C:\Users\student\Desktop\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 (stu_no,name,sex,tel) 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["stu_no"].Value;

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

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


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

                        ID = reader["識別碼"].ToString();
                        textBox1.Text = reader["stu_no"].ToString();
                        textBox2.Text = reader["name"].ToString();
                        textBox3.Text = reader["sex"].ToString();
                        textBox4.Text = reader["tel"].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["stu_no"].Value;
                       
                        connection.Open();
                        OleDbCommand command = new OleDbCommand();
                        command.Connection = connection;

               

                        command.CommandText = "delete from person WHERE stu_no = '" + 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 stu_no = '" + textBox1.Text + "'WHERE 識別碼 = " + ID ;

                      command.ExecuteNonQuery();

                      dAdapter.Fill(dTable);
        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }
    }
}

2015年12月4日 星期五

資料庫

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: 這行程式碼會將資料載入 'db1DataSet.DataTable2' 資料表。您可以視需要進行移動或移除。
            this.dataTable2TableAdapter.Fill(this.db1DataSet.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 textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void tabPage5_Click(object sender, EventArgs 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 (bindingSource4.Position + 1 < bindingSource4.Count)
                bindingSource4.MoveNext();

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

            // Force the form to repaint.
            this.Invalidate();
            //textBox1.DataBindings.Add("Text", bindingSource4, "chinese",true);
        }

        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 (bindingSource4.Position - 1 < 0)
                bindingSource4.MoveLast();

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

            // Force the form to repaint.
            this.Invalidate();
            //textBox1.DataBindings.Add("Text", bindingSource4, "chinese",true);
        }
       
        private void button3_Click(object sender, EventArgs e)
        {
           
            textBox1.DataBindings.Add("Text", bindingSource4, "chinese");
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

2015年11月20日 星期五

資料庫

將ACCESS做好的表單處存為副檔名為.mdb的檔案
從V#From1中使用bindingsource連結表單
從V#From1中加入datagridview,將表單內容顯示出來
工具箱→ bindingSource1→DataSoure→選擇檔案→DataMenber→選擇資料庫
工具箱→dataGridView1→選擇資料來源→bindingSource1

2015年11月6日 星期五

計算機

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 WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        int z;
        float e;
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

       

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (label2.Text == "".ToString())
            {
                float a = float.Parse(textBox1.Text);
                label3.Text = a.ToString();
                textBox1.Text = "".ToString();
                z = 1;
            }
            else
            {
                float r = float.Parse(label2.Text);
                label3.Text = r.ToString();
                z = 1;
            }
        }
        private void button2_Click_1(object sender, EventArgs e)
        {
            if (label2.Text == "".ToString())
            {
                float a = float.Parse(textBox1.Text);
                label3.Text = a.ToString();
                textBox1.Text = "".ToString();
                z = 2;
            }
            else
            {
                float r = float.Parse(label2.Text);
                label3.Text = r.ToString();
                z = 2;
            }
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            if (label2.Text == "".ToString())
            {
            float a = float.Parse(textBox1.Text);
            label3.Text = a.ToString();
            textBox1.Text = "".ToString();
            z = 3;
            }
            else
            {
                float r = float.Parse(label2.Text);
                label3.Text = r.ToString();
                z = 3;
            }
        }

        private void button4_Click_1(object sender, EventArgs e)
        {
            if (label2.Text == "".ToString())
            {
            float a = float.Parse(textBox1.Text);
            label3.Text = a.ToString();
            textBox1.Text = "".ToString();
            z = 4;
            }
            else
            {
                float r = float.Parse(label2.Text);
                label3.Text = r.ToString();
                z = 4;
            }
        }

        private void label3_Click_1(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {
            float q = float.Parse(label3.Text),w = float.Parse(textBox1.Text);          
            if (z==1)
            {
                label2.Text = (q + w).ToString();
               
            }
            if (z == 2)
            {
                label2.Text = (q - w).ToString();
            }
            if (z == 3)
            {
                label2.Text = (q * w).ToString();
            }
            if (z == 4)
            {
                label2.Text = (q / w).ToString();
            }
            textBox1.Text = "".ToString();
            label3.Text = "".ToString();
            //float r = float.Parse(label2.Text);
            //label3.Text = r.ToString();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + "0";
        }

        private void button7_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + "1";
        }

        private void button8_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + "2";
        }

        private void button9_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + "3";
        }

        private void button10_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + "4";
        }

        private void button11_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + "5";
        }

        private void button12_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + "6";
        }

        private void button13_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + "7";
        }

        private void button14_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + "8";
        }

        private void button15_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + "9";
        }

        private void button16_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            label2.Text = "";
        }

        private void button17_Click(object sender, EventArgs e)
        {
            if (label2.Text == "".ToString())
            {
                float a = float.Parse(textBox1.Text);
                textBox1.Text = (1 / a).ToString();
            }
            else
            {
                float a = float.Parse(label2.Text);
                label2.Text = (1 / a).ToString();
            }
        }
    }
}

2015年10月30日 星期五

推盤2.0

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
    {
        Button[,] Buttons = new System.Windows.Forms.Button[5, 5];
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 1; i < 5; i++)
            {
                for (int j = 1; j < 5; 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)
        {
            Random rnd = new Random();
            int[] a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
            for (int i = 0; i < 16; ++i)
            {
                int j = rnd.Next(15);
                int t = a[j];
                a[j] = a[i];
                a[i] = t;
            }
            int k = 0, c, v;
            for (int m = 1; m < 5; m++)
            {
                for (int n = 1; n < 5; n++)
                {

                    Buttons[m, n].Text = a[k].ToString();
                    k = k + 1;
                }
            }
            for (int b = 0; b < 16; ++b)
            {
                if (a[b] == 0)
                {
                    c = b % 4 + 1;
                    v = b / 4 + 1;
                    Buttons[v, c].Text = "";
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int x = 0;
            for (int s = 1; s < 5; s++)
            {
                for (int d = 1; d < 5; d++)
                {
                    Buttons[d, s].Text = x.ToString();
                    x = x + 1;
                }
            }
        }
    }
}

2015年10月23日 星期五

推盤遊戲 D0250623 廖啟淵

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 r1 = 0;
        int r2 = 0;
        int r3 = 0;
        int r4 = 0;
        int r5 = 0;
        int r6 = 0;
        int r7 = 0;
        int r8 = 0;
        int r9 = 0;
        public Form1()
        {
            InitializeComponent();
            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;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button7_Click(object sender, EventArgs e)
        {

        }

        private void button6_Click(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void button10_Click(object sender, EventArgs e)
        {
            Random rnd = new Random();
            int[] a = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
            for (int i = 0; i < 9; ++i)
            {
                int j = rnd.Next(8);
                int t = a[j];
                a[j] = a[i];
                a[i] = t;
            }
            button1.Text = a[0].ToString();
            button2.Text = a[1].ToString();
            button3.Text = a[2].ToString();
            button4.Text = a[3].ToString();
            button5.Text = a[4].ToString();
            button6.Text = a[5].ToString();
            button7.Text = a[6].ToString();
            button8.Text = a[7].ToString();
            button9.Text = a[8].ToString();
            //button10.Enabled = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }
    }
}

2015年10月2日 星期五

程式設計工藝大師 D0250623 廖啟淵

程式設計工藝大師
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, b, c, r,t = 0;

        public Form1()
        {
            InitializeComponent();
        }
       
        private void button2_Click(object sender, EventArgs e)
        {
            
        }

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

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

        private void timer1_Tick(object sender, EventArgs e)
        {
            t = t + 1;
            r = t % 6;
            b = r;
            button2.Text = b.ToString();
            if (b == 3)
            {
                button2.BackColor = System.Drawing.Color.FromName("Yellow");
            }
            else
            {
                button2.BackColor = System.Drawing.Color.FromName("White");
            }
            
            a = r;
            button1.Text = a.ToString();
            if (a > 3)
            {
                button1.BackColor = System.Drawing.Color.FromName("Red");
            }
            else 
            {
                button1.BackColor = System.Drawing.Color.FromName("White");
            }
            
            c = r;
            button3.Text = c.ToString();
            if (c < 3)
            {
                button3.BackColor = System.Drawing.Color.FromName("Green");
            }
            else
            {
                button3.BackColor = System.Drawing.Color.FromName("White");
            }
        }
    }
}