⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 neworder.cs

📁 微软的行业应用解决方案实例!非常优秀的Windows Mobile开发案例
💻 CS
字号:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using HardwareDistributor.Business;

namespace HardwareDistributor.UI
{
    /// <summary>
    /// UI layer form that facilitates the creation of new orders
    /// </summary>
    public partial class NewOrder : Form
    {
        private decimal _totalPrice = 0;
        private Business.CustomerCollection _customers = null;
        private Business.InventoryCollection _inventory = null;

        public NewOrder()
        {
            InitializeComponent();
        }


        /// <summary>
        /// Cancels the order
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemCancel_Click(object sender, EventArgs e)
        {
            //Close form
            this.Close();
        }


        /// <summary>
        /// This menu creates an order based on the user's selections
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemCreate_Click(object sender, EventArgs e)
        {
            //Ensure that items have been added to the order first
            if (listView1.Items.Count > 0)
            {
                try
                {
                    //Get Customer Id
                    Business.Customer _customer = (Business.Customer)cboCustomer.SelectedItem;
                    
                    //Get Delivery Date
                    string _deliveryDate = dateTimePicker1.Value.ToShortDateString();

                    //Set OrderState to Placed
                    int _orderState = 1;

                    //Get collection of items in ListView
                    ListView.ListViewItemCollection _orderItems = listView1.Items;
                    
                    //Insert into the Orders and OrderDetails tables
                    Business.Services.CreateOrder(_customer.CustomerId, _deliveryDate, _orderState, _orderItems);

                    //Set Notification Balloon caption
                    notification1.Caption = "New Order";
                    //Set Notification Balloon text
                    notification1.Text = "A new order has been placed.  Please synchronize your device to make this order available to everyone else.";
                    //Set duration that Notification Balloon will be visible
                    notification1.InitialDuration = 5;
                    //Set Notification Balloon to visible
                    Application.DoEvents();
                    notification1.Visible = true;
                    Application.DoEvents();

                    //Close Form
                    this.Close();
                }
                catch (Exception ex)
                {
                    Business.Services.LogErrors(ex);
                    MessageBox.Show("Error creating order", "System Error");
                }
            }
            else
            {
                MessageBox.Show("You must add hardware items to your order", "Empty Order");
            }
        }


        /// <summary>
        /// Load the NewOrder form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NewOrder_Load(object sender, EventArgs e)
        {
            try
            {
                //Set default delivery date to tomorrow
                dateTimePicker1.MinDate = System.DateTime.Today.AddDays(1);

                //Retrieve CustomerCollection
                _customers = Business.GlobalCache.Instance.Customers;
                //Retrieve InventoryCollection
                _inventory = Services.GetInventory();
                
                //Display customer information
                cboCustomer.DisplayMember = "CustomerName";
                cboCustomer.ValueMember = "CustomerId";
                cboCustomer.DataSource = _customers;

                //Display inventory items
                cboInventoryItems.DisplayMember = "InventoryName";
                cboInventoryItems.ValueMember = "InventoryId";
                cboInventoryItems.DataSource = _inventory;
            }
            catch (Exception ex)
            {
                Business.Services.LogErrors(ex);
                MessageBox.Show("Error displaying customers and inventory items", "System Error");
            }
        }

        
        /// <summary>
        /// Add item to listview
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                //Get Quantity
                int _quantity = (int)numericUpDownQty.Value;

                //Get selected Inventory object
                Business.Inventory _inventory = (Business.Inventory)cboInventoryItems.SelectedItem;

                //Get item price
                decimal _price = _quantity * _inventory.Price;

                //Get total price
                _totalPrice += _price;


                //Verify there's enough hardware left in stock
                if (_inventory.InStock >= _quantity)
                {
                    //Add Quantity
                    ListViewItem lvi = new ListViewItem(_quantity.ToString());
                    //Add Inventory Name
                    lvi.SubItems.Add(_inventory.InventoryName);
                    //Add Inventory ID
                    lvi.SubItems.Add(_inventory.InventoryId.ToString());
                    //Add ListView Items to ListView
                    listView1.Items.Add(lvi);
                    //Update column header
                    listView1.Columns[1].Text = "Hardware   $ " + _totalPrice.ToString();
                }
                else
                {
                    MessageBox.Show("The quantity you requested exceeds our quantity in stock", "Quantity exceeds stock");
                }
            }
            catch (Exception ex)
            {
                Business.Services.LogErrors(ex);
                MessageBox.Show("Error adding items", "System Error");
            }
        }


        /// <summary>
        /// Removes items from the listview
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRemove_Click(object sender, EventArgs e)
        {
            try
            {
                //Retrieve a collection of items from the list view control
                ListView.ListViewItemCollection checkedItems = listView1.Items;
                //Iterate through the collection
                foreach (ListViewItem item in checkedItems)
                {
                    //Determine of item is checked
                    if (item.Checked)
                    {
                        int _qty = int.Parse(item.SubItems[0].Text);
                        int _inventoryId = int.Parse(item.SubItems[2].Text);

                        //Get price based on InventoryId
                        decimal _price = Business.Services.GetPrice(_inventoryId);

                        //Get Subtotal based on Qty
                        decimal _subTotal = _qty * _price;

                        //Subtract price from Total Price
                        _totalPrice -= _subTotal;

                        //Update column header
                        listView1.Columns[1].Text = "Hardware   $ " + _totalPrice.ToString();

                        //Remove item from list
                        listView1.Items.Remove(item);
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                Business.Services.LogErrors(ex);
                MessageBox.Show("Error removing hardware item", "System Error");
            }
        }


        /// <summary>
        /// Fires when user selects a different item in the combo box
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cboInventoryItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                //Get selected Inventory object
                Business.Inventory inventory = (Business.Inventory)cboInventoryItems.SelectedItem;

                //Display price
                lblPrice.Text = "$ " + inventory.Price.ToString();

                //Display hardware picture in PictureBox
                pictureBox1.Image = new Bitmap(inventory.PictureAsStream);
            }
            catch (Exception ex)
            {
                Business.Services.LogErrors(ex);
                MessageBox.Show("Error displaying hardware picture", "System Error");
            }
        }

       
        /// <summary>
        /// Launch Help
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void linkLabelHelp_Click_1(object sender, EventArgs e)
        {
            try
            {
                //Show New Order Help
                Help.ShowHelp(this, @Business.GlobalCache.Instance.AppPath + "HardwareHelp.htm#neworder");
            }
            catch (Exception ex)
            {
                Business.Services.LogErrors(ex);
                MessageBox.Show("Error displaying Help", "System Error");
            }

        }


        /// <summary>
        /// This event fires when the state of the Notification Balloon changes
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void notification1_BalloonChanged(object sender, Microsoft.WindowsCE.Forms.BalloonChangedEventArgs e)
        {
            //If Balloon is not visible
            if (!e.Visible)
            {
                //Dispose of that Notification Balloon object
                ((Microsoft.WindowsCE.Forms.Notification)sender).Dispose();
            } 
        }

      
        
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -