2014年2月21日 星期五

[C#] 飲料店POS



學習 動態產生元件、委派、共用一個事件
學習 dataGridView
學習 轉換型別

------------------------------------------------
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 POS
{
    public partial class Form1 : Form
    {
        private string sel_Name;
        private int    sel_Name_index;
        private string sel_Sugar;
        private string sel_Ice;
        private int    sel_Size; // 大中小,用index記錄
        private double sel_Price;
        private int    sel_Number;
        private double sel_Subtotal;

        public Form1()
        {
            InitializeComponent();

            // DataGridView 銷售項目
            DataGridViewRowCollection rows = dataGridViewMenu.Rows;
            rows.Add(new Object[] { "泡沫紅茶", 20, 15, -1 });
            rows.Add(new Object[] { "阿薩姆紅茶", 20, 15, -1 });
            rows.Add(new Object[] { "茉香綠茶", 20, 15, -1 });
            rows.Add(new Object[] { "文山青茶", 25, 20, -1 });
            rows.Add(new Object[] { "凍頂烏龍茶", 25, 20, -1 });
            rows.Add(new Object[] { "洛神花茶", 30, 25, -1 });
            rows.Add(new Object[] { "仙草甘茶", 30, 25, -1 });
            rows.Add(new Object[] { "珍珠綠茶", 30, 20, -1 });
            rows.Add(new Object[] { "珍珠紅茶", 30, 20, -1 });
            rows.Add(new Object[] { "珍珠青茶", 35, 25, -1 });
            rows.Add(new Object[] { "珍珠桂花茶", 35, 25, -1 });
            rows.Add(new Object[] { "珍珠奶茶", 45, 35, 30 });
            rows.Add(new Object[] { "珍珠奶綠", 45, 35, 30 });
            rows.Add(new Object[] { "椰果奶茶", 45, 35, 30 });
            rows.Add(new Object[] { "布丁奶茶", 45, 35, 30 });
            rows.Add(new Object[] { "仙草凍奶茶", 45, 35, 30 });
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Button[] lbtn = new Button[40]; // 動態產生Button
            for (int i = 0; i < 24; i++)
            {
                if (i < dataGridViewMenu.Rows.Count && dataGridViewMenu.Rows[i].Cells[0].Value != null)
                {
                    lbtn[i] = new Button();
                }
                else
                {
                    break;
                }
                lbtn[i].Name = "linkButton" + i;
                lbtn[i].Text = dataGridViewMenu.Rows[i].Cells[0].Value.ToString();
                lbtn[i].Height = 50;
                lbtn[i].Width = 100;
                lbtn[i].Left = i % 3 * lbtn[i].Width;
                lbtn[i].Top = i / 3 * lbtn[i].Height;
                lbtn[i].Click += new EventHandler(linkbutton_Click); // 共用一個事件
                panelMenu.Controls.Add(lbtn[i]);
            }
        }

        // 選取數量時,自動計算小結
        private void calculateSubTotal()
        {
            sel_Number = (int)numericUpDownNumber.Value;
            sel_Subtotal = sel_Price * (double)sel_Number;
            textBoxSubtotal.Text = sel_Subtotal.ToString();
        }

        // 動態產生的Button,共用一個事件
        private void linkbutton_Click(object sender, EventArgs e)
        {
            Button getButton = sender as Button; // 將sender參數轉型成Button,得知按了哪一個

            // 更動品名時,預設為大杯
            sel_Name_index = getButton.TabIndex; // 更新品名
            sel_Size = listBoxLMS.SelectedIndex = listBoxLMS.FindString("大杯");
            labelSize.Text = "大杯";
                        
            sel_Name          = Convert.ToString(dataGridViewMenu.Rows[sel_Name_index].Cells[0].Value);
            labelName.Text    = Convert.ToString(dataGridViewMenu.Rows[sel_Name_index].Cells[0].Value);
            sel_Price         = Convert.ToDouble(dataGridViewMenu.Rows[sel_Name_index].Cells[sel_Size + 1].Value); // 參考 sel_Size 載入價格
            textBoxPrice.Text = Convert.ToString(dataGridViewMenu.Rows[sel_Name_index].Cells[sel_Size + 1].Value);

            calculateSubTotal();
        }
        
        private void numericUpDownNumber_ValueChanged(object sender, EventArgs e)
        {
            calculateSubTotal();
        }

        private void listBoxSugar_SelectedIndexChanged(object sender, EventArgs e)
        {
            sel_Sugar = listBoxSugar.SelectedItem.ToString();
            labelSugar.Text = listBoxSugar.SelectedItem.ToString();
        }

        private void listBoxIce_SelectedIndexChanged(object sender, EventArgs e)
        {
            sel_Ice = listBoxIce.SelectedItem.ToString();
            labelIce.Text = listBoxIce.SelectedItem.ToString();
        }

        private void listBoxLMS_SelectedIndexChanged(object sender, EventArgs e)
        {
            double price = Convert.ToDouble(dataGridViewMenu.Rows[sel_Name_index].Cells[listBoxLMS.SelectedIndex + 1].Value);
            if (price < 0)
            {
                MessageBox.Show("此品項不賣此大小");
                labelSize.Text = ".........";
                sel_Size = 0;
                return;
            }

            sel_Size       = listBoxLMS.SelectedIndex; // 更新大小杯
            labelSize.Text = listBoxLMS.SelectedItem.ToString();
            sel_Price         = Convert.ToDouble(dataGridViewMenu.Rows[sel_Name_index].Cells[sel_Size + 1].Value); // 參考 sel_Size 載入價格
            textBoxPrice.Text = Convert.ToString(dataGridViewMenu.Rows[sel_Name_index].Cells[sel_Size + 1].Value);

            calculateSubTotal();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            if ( labelName.Text == ".........") { MessageBox.Show("品名尚未確定"); return; }
            if (labelSugar.Text == ".........") { MessageBox.Show("甜度尚未確定"); return; }
            if (  labelIce.Text == ".........") { MessageBox.Show("冰塊尚未確定"); return; }
            if ( labelSize.Text == ".........") { MessageBox.Show("大小尚未確定"); return; }
                
            calculateSubTotal(); // 計算小計

            listBoxLMS.SelectedIndex = sel_Size; // 以值為主
            dataGridViewOrder.Rows.Add(new Object[] { sel_Name, sel_Sugar, sel_Ice, listBoxLMS.SelectedItem.ToString(), sel_Price, sel_Number, sel_Subtotal }); // 加入訂單
            calculateTotal(); // 計算總金額

            // 初始
            labelName.Text = ".........";
            labelSugar.Text = ".........";
            labelIce.Text = ".........";
            labelSize.Text = ".........";
            textBoxPrice.Text = "";
            numericUpDownNumber.Value = 1;
            textBoxSubtotal.Text = "";
        }

        private void calculateTotal()
        {
            int cup = 0;
            double total = 0.0;
            for (int i = 0; i < dataGridViewOrder.Rows.Count; i++)
            {
                DataGridViewRow row = dataGridViewOrder.Rows[i];
                if (row.Cells[0].Value != null)
                {
                    cup += (int)row.Cells[5].Value;
                    total += (double)row.Cells[6].Value;
                }
            }
            textBoxCup.Text = cup.ToString();
            textBoxTotal.Text = total.ToString();
        }

        private void buttonOrder_Click(object sender, EventArgs e)
        {
            dataGridViewOrder.Rows.Clear();
            textBoxCup.Text = "";
            textBoxTotal.Text = "";
        }

    }
}

沒有留言: