2015年10月2日 星期五

程式設計物件導向C#

程式設計工藝大師

物件導向C# - 紅綠燈
(Microsoft Visual Studio 2012)


















namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int c = 0, d;
        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 = d.ToString();
            button3.Text = d.ToString();
            button4.Text = d.ToString();
            if (d == 1)
            {
                button1.BackColor = System.Drawing.Color.Green;
            }
            else
            {
                button1.BackColor = System.Drawing.Color.Black;
            }
            if (d == 2)
            {
                button3.BackColor = System.Drawing.Color.Yellow;
            }
            else
            {
                button3.BackColor = System.Drawing.Color.Black;
            }
            if (d == 0)
            {
                button4.BackColor = System.Drawing.Color.Red;
            }
            else
            {
                button4.BackColor = System.Drawing.Color.Black;
            }
        }
    }
}

 物件導向C# - 拉霸機
(Microsoft Visual Studio 2012)







namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int c1 = 0, d1 = 0;
        int c2 = 0, d2 = 0;
        int c3 = 0, d3 = 0;
        int rndmoney1 = 100;
        int rndmoney2 = 100;
        int rndmoney3 = 100;
        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            c1 = c1 + 1;
            d1 = c1 % 10;
            if (c1 >= rndmoney1) timer1.Enabled = false;
            if(d1==0)
            {
                //button1.BackColor = Color.Green;
            }
            else
            {
                //button1.BackColor = Color.Black;
            }
            if (d1 == 1)
            {
                //button2.BackColor = Color.Yellow;
            }
            else
            {
                //button2.BackColor = Color.Black;
            }
            if(d1==2)
            {
                 //button3.BackColor = Color.Red;
            }
            else
            {
                 //button3.BackColor = Color.Black;
            }
            button1.Text = d1.ToString();
         
       
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {

        }
         

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

        private void button4_Click(object sender, EventArgs e)
        {
            c1 = 0;
            c2 = 0;
            c3 = 0;
            timer1.Enabled = true;
            timer2.Enabled = true;
            timer3.Enabled = true;
            Random rnd = new Random();
            rndmoney1 = rnd.Next(1, 101);
            rndmoney2 = rnd.Next(1, 101);
            rndmoney3 = rnd.Next(1, 101);
            button4.Text = rndmoney1.ToString() + rndmoney2.ToString() + rndmoney3.ToString();
         


        }

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

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



物件導向C# - 9個數字不重複
(Microsoft Visual Studio 2012)



namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int  rndmoney1=0;
        int rndmoney2 = 0;
        int rndmoney3 = 0;
        int rndmoney4 = 0;
        int rndmoney5 = 0;
        int rndmoney6 = 0;
        int rndmoney7 = 0;
        int rndmoney8= 0;
        int rndmoney9 = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void button9_Click(object sender, EventArgs e)
        {

        }

        private void button10_Click(object sender, EventArgs e)
        {
             Random rnd = new Random();
            rndmoney1 = rnd.Next(0, 9); // creates a number between 0 and 9
            rndmoney2 = rnd.Next(0, 9); // creates a number between 0 and 9
            rndmoney3 = rnd.Next(0, 9); // creates a number between 0 and 9
            rndmoney4 = rnd.Next(0, 9); // creates a number between 0 and 9
            rndmoney5 = rnd.Next(0, 9); // creates a number between 0 and 9
            rndmoney6 = rnd.Next(0, 9); // creates a number between 0 and 9
            rndmoney7 = rnd.Next(0, 9); // creates a number between 0 and 9
            rndmoney8 = rnd.Next(0, 9); // creates a number between 0 and 9
            rndmoney9 = rnd.Next(0, 9); // creates a number between 0 and 9
            button1.Text = rndmoney1.ToString();
            button2.Text = rndmoney2.ToString();
            while (rndmoney1 == rndmoney2)
            {
                //MessageBox.Show("錯誤");
                rndmoney2 = rnd.Next(0, 9); // creates a number between 0 and 9
                button2.Text = rndmoney2.ToString();
            }
            button3.Text = rndmoney3.ToString();
            while (rndmoney1 == rndmoney3 || rndmoney2 == rndmoney3)
            {
                //MessageBox.Show("錯誤");
                rndmoney3 = rnd.Next(0, 9); // creates a number between 0 and 9
                button3.Text = rndmoney3.ToString();
            }
            button4.Text = rndmoney4.ToString();
            while (rndmoney1 == rndmoney4 || rndmoney2 == rndmoney4 || rndmoney3 == rndmoney4)
            {
                rndmoney4 = rnd.Next(0, 9); // creates a number between 0 and 9
                button4.Text = rndmoney4.ToString();
            }
            button5.Text = rndmoney5.ToString();
            while (rndmoney1 == rndmoney5 || rndmoney2 == rndmoney5 || rndmoney3 == rndmoney5 || rndmoney4 == rndmoney5)
            {
                rndmoney5 = rnd.Next(0, 9); // creates a number between 0 and 9
                button5.Text = rndmoney5.ToString();
            }
            button6.Text = rndmoney6.ToString();
            while (rndmoney1 == rndmoney6 || rndmoney2 == rndmoney6 || rndmoney3 == rndmoney6 || rndmoney4 == rndmoney6 || rndmoney5 == rndmoney6)
            {
                rndmoney6 = rnd.Next(0, 9); // creates a number between 0 and 9
                button6.Text = rndmoney6.ToString();
            }
            button7.Text = rndmoney7.ToString();
            while (rndmoney1 == rndmoney7 || rndmoney2 == rndmoney7 || rndmoney3 == rndmoney7 || rndmoney4 == rndmoney7 || rndmoney5 == rndmoney7 || rndmoney6 == rndmoney7)
            {
                rndmoney7 = rnd.Next(0, 9); // creates a number between 0 and 9
                button7.Text = rndmoney7.ToString();
            }
            button8.Text = rndmoney8.ToString();
            while (rndmoney1 == rndmoney8 || rndmoney2 == rndmoney8 || rndmoney3 == rndmoney8 || rndmoney4 == rndmoney8 || rndmoney5 == rndmoney8 || rndmoney6 == rndmoney8 || rndmoney7 == rndmoney8)
            {
                rndmoney8 = rnd.Next(0, 9); // creates a number between 0 and 9
                button8.Text = rndmoney8.ToString();
            }
            button9.Text = rndmoney9.ToString();
            while (rndmoney1 == rndmoney9 || rndmoney2 == rndmoney9 || rndmoney3 == rndmoney9 || rndmoney4 == rndmoney9 || rndmoney5 == rndmoney9 || rndmoney6 == rndmoney9 || rndmoney7 == rndmoney9 || rndmoney8 == rndmoney9)
            {
                rndmoney9 = rnd.Next(0, 9); // creates a number between 0 and 9
                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;*/
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }
    }
}


物件導向C# - 十六宮格
(Microsoft Visual Studio 2012)




namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int i = 1, j = 1;
        int pro = 0;
        Button[,] Buttons = new System.Windows.Forms.Button[5, 5];

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for (i = 1; i < 5; i++)
            {
                for (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)
        {
            for (i = 1; i < 5; i++)
            {
                for (j = 1; j < 5; j++)
                {
                    pro = (j-1)*4 + i;
                    Buttons[i, j].Text =pro.ToString();
                }
            }
        }
    }
}



物件導向C# - 計算機(+,-,*,/)
(Microsoft Visual Studio 2012)








namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        string b1 ,b2;
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            float c1;
            b1 = textBox1.Text;
            b2 = textBox2.Text;
            float a1 = float.Parse(b1);
            float a2 = float.Parse(b2);
            c1 = a1 + a2;
            label2.Text = c1.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            float c1;
            b1 = textBox1.Text;
            b2 = textBox2.Text;
            float a1 = float.Parse(b1);
            float a2 = float.Parse(b2);
            c1 = a1 - a2;
            label2.Text = c1.ToString();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            float c1;
            b1 = textBox1.Text;
            b2 = textBox2.Text;
            float a1 = float.Parse(b1);
            float a2 = float.Parse(b2);
            c1 = a1 * a2;
            label2.Text = c1.ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            float c1;
            b1 = textBox1.Text;
            b2 = textBox2.Text;
            float a1 = float.Parse(b1);
            float a2 = float.Parse(b2);
            c1 = a1 * a2;
            label2.Text = c1.ToString();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            
            float c1;
            b1 = textBox1.Text;
            b2 = textBox2.Text;
            float a1 = float.Parse(b1);
            float a2 = float.Parse(b2);
            c1 = a1 / a2;
            label2.Text = c1.ToString();
            if (a2 == 0)
            {
                label2.Text = "除數不可為零";
            }
        }
    }
}


物件導向C# - 資料庫
office Access 2013

打開範例搜尋北風→複製客戶清單到EXCEL→重Access令存新檔成2000年的mdb檔→存放C槽

Visual studio 2012

工具箱→bindingSource1→DataSoure→選擇檔案→DataMenber→選擇資料庫
工具箱→dataGridView1→選擇資料來源→bindingSource1


使用文字方塊Textbox顯示資料庫資料
textBox1.DataBindings.Add("Text", bindingSource4, "chinese");



物件導向C# - 資料庫新增修改刪除

Visual studio 2010




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 WindowsFormsApplication2
{
    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 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["stu_no"].ToString();
                        textBox3.Text = reader["tel"].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 button1_Click(object sender, EventArgs e)
        {

            connection.Open();
            dTable.Clear();
            OleDbCommand command2 = new OleDbCommand();
            command2.Connection = connection;

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

            command2.ExecuteNonQuery();

            dAdapter.Fill(dTable);

            connection.Close();
               
        }

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

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


沒有留言:

張貼留言