📄 additem.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using HardwareDistributor.Properties;
using Microsoft.WindowsCE.Forms;
namespace HardwareDistributor.UI
{
public partial class AddItem : Form
{
#region Members
private NewOrder _parent; //holds the reference to the parent form
private List<Business.Inventory> inventoryList = null;
//use precompiled Reg expression for performance
private static Regex notNaturalNumber = new Regex("[^0-9]");
private Regex isNaturalNumber = new Regex("0*[1-9][0-9]*");
#endregion
#region Constructors
/// <summary>
/// constructor
/// </summary>
/// <param name="source"></param>
public AddItem(NewOrder source)
{
InitializeComponent();
// Keeping the parent so we know where to add the item that the user picked
_parent = source;
// 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;
InputModeEditor.SetInputMode(quantityTextBox, InputMode.Numeric);
}
#endregion
#region Event Handlers
/// <summary>
/// Fires when add soft key is clicked, so add the item to the parent dialog
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void menuItem2_Click(object sender, EventArgs e)
{
AddInventoryItem();
}
/// <summary>
/// Fires when the object is loaded
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void AddItem_Load(object sender, EventArgs e)
{
BindInventoryDisplay();
}
/// <summary>
/// Fires if the close soft key is clicked
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void menuItemCancel_Click(object sender, EventArgs e)
{
this.Close();
}
/// <summary>
/// auto select the contents of the text box
/// </summary>
/// <param name="sender">NA</param>
/// <param name="e">NA</param>
private void quantityTextBox_GotFocus(object sender, EventArgs e)
{
quantityTextBox.SelectAll();
}
/// <summary>
/// Custom Handling for the back key
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BackKeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyCode == System.Windows.Forms.Keys.Back))
{
// Back
// if the back key is hit, close the dialog
this.Close();
}
}
#endregion
#region Methods
/// <summary>
/// Fires when user selects a different item in the combo box
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void InventoryItemsCombobox_SelectedIndexChanged(object sender, EventArgs e)
{
UpdateInventoryItemDetails();
}
/// <summary>
/// update the selected inventory item's price and display image
/// </summary>
private void UpdateInventoryItemDetails()
{
//get the object
Business.Inventory inventory = (Business.Inventory)InventoryItemsComboBox.SelectedItem;
//update the price
priceLabel.Text = "$ " + inventory.Price.ToString();
try
{
//Display hardware picture in PictureBox
Bitmap currentImage = new Bitmap(inventory.PictureAsStream);
//adjust height according to picure box dimensions
if (currentImage.Height > pictureBox1.Height)
{
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Width = currentImage.Width;
}
else
{
pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
pictureBox1.Width = this.Width;
}
pictureBox1.Image = currentImage;
}
catch (Exception ex)
{
Business.Services.LogErrors(ex);
MessageBox.Show(Resources.DisplayPictureError, Resources.SystemError);
}
}
/// <summary>
/// Adds inventory item to the list
/// </summary>
private void AddInventoryItem()
{
string qty = quantityTextBox.Text;
//if valid number
if (!notNaturalNumber.IsMatch(qty) && isNaturalNumber.IsMatch(qty))
{
//extract number
int quantity = System.UInt16.Parse(quantityTextBox.Text);
//Get selected Inventory object
Business.Inventory inventory = (Business.Inventory)InventoryItemsComboBox.SelectedItem;
//invoke parent with inventory item data
if (_parent.AddInventoryItem(inventory, quantity))
{
this.Close();
}
}
else
{
MessageBox.Show(Resources.NumberValidationMsg);
}
}
/// <summary>
/// binds the combo box with the inventory items list
/// </summary>
private void BindInventoryDisplay()
{
//Retrieve InventoryCollection
inventoryList = Business.Services.GetInventory();
//Display inventory items
InventoryItemsComboBox.DisplayMember = "InventoryName";
InventoryItemsComboBox.ValueMember = "InventoryId";
InventoryItemsComboBox.DataSource = inventoryList;
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -