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

📄 neworder.cs

📁 微软的行业应用解决方案示例
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using HardwareDistributor.Business;
using System.Collections.Generic;
using HardwareDistributor.Properties;
using HardwareDistributor.Synchronization;
using System.ServiceModel.Channels;

namespace HardwareDistributor.UI
{
    /// <summary>
    /// UI layer form that facilitates the creation of new orders
    /// </summary>
    public partial class NewOrder : Form
    {
        #region Members

        private decimal _totalPrice = 0;
        private List<Customer> _customers = null;
        private HardwareDistributor.Controls.GradientHeaders header;

        #endregion

        #region Contructors

        public NewOrder()
        {
            InitializeComponent();

            // We are removing both the Control Box and the Minimize Box, so that we don't
            // have to handle these in our code. This is not necessary in Windows Mobile Standard,
            // but we are writing this code so that it works on all platforms without any changes
            // or any recompiling.
            this.ControlBox = false;
            this.MinimizeBox = false;

            // Need to adjust the column width, so that we automatically adjust to different screen
            // sizes and orientations.
            this.columnHeaderItem.Width = this.ordersListView.Width - this.columnHeaderQty.Width - 8;

            AttachHeader();

            HandleBackgroundSync();
        }

        #endregion

        #region Event Handlers

        void ClientSyncAgent_SyncEnd(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.Default;
        }

        void ClientSyncAgent_SyncBegin(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
        }

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

        /// <summary>
        /// Load the NewOrder form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NewOrder_Load(object sender, EventArgs e)
        {
            SetDefaultDeliveryDate();

            RefreshCustomerList();
        }

        /// <summary>
        /// show add item form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemAdd_Click(object sender, EventArgs e)
        {
            // launch dialog to add an item
            AddItem addItemForm = new AddItem(this);

            addItemForm.ShowDialog();

            addItemForm.Dispose();

            //bring us back in focus
            this.Activate();

        }

        /// <summary>
        /// This menu creates an order based on the user's selections
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemCreateOrder_Click(object sender, EventArgs e)
        {
            bool orderCreatedInLocalDB = false;

            //Ensure that items have been added to the order first
            if (ordersListView.Items.Count == 0)
            {
                MessageBox.Show(Resources.MustAddHardwareMessage, Resources.EmptyOrder);
                return;
            }

            //Validations passed

            try
            {
                //prepare the user for a wait
                Cursor.Current = Cursors.WaitCursor;
                Application.DoEvents();

                //create order in local DB
                DisplayStatus(Resources.UpdateOrderMsg, 0);
                Application.DoEvents();
                CreateOrder();
                Application.DoEvents();

                //flag that we have the order in local database
                orderCreatedInLocalDB = true;

                //check if we have network connectivity
                DisplayStatus(Resources.CheckNetworkMessage, 0);
                bool networkAvailable = Business.Services.GetConnectionStatus();
                Application.DoEvents();

                //try to sync only if network is available
                if (networkAvailable)
                {
                    //Test to see if Replication URL is reachable
                    DisplayStatus(Resources.checkIisMessage, 0); ;
                    bool replicationAvailable = Business.Services.CheckSynchronizationUrl();
                    Application.DoEvents();

                    if (replicationAvailable)
                    {
                        //send changes down to server DB
                        DisplayStatus(Resources.SyncDbMessage, 0);
                        Application.DoEvents();
                        Synchronize();
                        Application.DoEvents();

                        //send notification to next role in workflow
                        DisplayStatus(Resources.SendingNotificationMessage, 0);
                        Application.DoEvents();
                        SendNotification();
                        Application.DoEvents();
                    }
                    else
                    {
                        //Replication URL isn't reachable
                        DisplayStatus(Resources.IisUnavailableMessage, 1000);
                    }
                }
                else
                {
                    DisplayStatus(Resources.NetworkUnavailableMessage, 1000);
                }

                this.Close();
            }
            catch (Utilities.WrapperException wex)
            {
                Cursor.Current = Cursors.Default;
                Application.DoEvents();

                //log and display error
                Business.Services.LogErrors(wex.InnerException);
                MessageBox.Show(wex.UserDisplayMessage, Resources.SystemError);
            }

            catch (Exception ex)
            {
                Cursor.Current = Cursors.Default;
                Application.DoEvents();

                Business.Services.LogErrors(ex);
                MessageBox.Show(ex.Message, Resources.SystemError);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
                ClearStatus();
                Application.DoEvents();

                //take him back to customer service screen
                if (orderCreatedInLocalDB)
                    this.Close();
            }

        }

        /// <summary>
        /// Synchronizes local db with server DB
        /// </summary>
        private void Synchronize()
        {
            try
            {
                Business.Services.SyncWithDataSource();
            }
            catch (Exception ex)
            {
                throw new Utilities.WrapperException(Resources.SyncDbError, ex);
            }

        }

        /// <summary>
        /// sends notification to the warehouse role that the order has been placed
        /// </summary>
        private void SendNotification()
        {
            try
            {
                Business.Services.SendNotification("Warehouse");
            }
            catch (Exception ex)
            {
                throw new Utilities.WrapperException(Resources.NotificationError, ex);
            }
        }


        /// <summary>
        /// Removes items from the listview
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemRemove_Click(object sender, EventArgs e)
        {
            //Get the select item
            ListView.ListViewItemCollection selectedItems = ordersListView.Items;
            foreach (ListViewItem item in selectedItems)
            {
                if (item.Selected)
                {
                    //and remove from list
                    RemoveInventoryItem(item);

                    break;
                }
            }
        }

        /// <summary>
        /// Handle the key down event for the listview, so that we can set focus to the next control
        /// when we hit the first or last item in the list.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ordersListView_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up &&
                            (ordersListView.Items.Count == 0 ||
                            (ordersListView.SelectedIndices.Count > 0 && ordersListView.SelectedIndices[0] == 0)))
            {
                e.Handled = true;
                this.SelectNextControl(ordersListView, false, true, false, true);
            }
            else if (e.KeyCode == Keys.Down &&
                            (ordersListView.Items.Count == 0 ||

⌨️ 快捷键说明

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