📄 inventory.cs
字号:
this.dvInventory.RowFilter = String.Empty;
if (0 == this.dvInventory.Count)
{
MessageBox.Show("No inventory", "IBuySpy Delivery");
return false;
}
this.dvInventory.RowFilter = String.Format("CategoryID = '{0}'", this.categoryID);
try
{
/// Update the user interface controls.
///
UpdateControls();
}
catch(Exception e)
{
MessageBox.Show(e.Message, "IBuySpy Delivery");
}
return true;
}
/// Update the user interface controls.
///
internal void UpdateControls()
{
DataRowView row = null;
int maxQuantity = 0;
/// If there is no order selected, the user navigated to the Inventory control by using the menu.
/// In this case, the user cannot add a product because there is no order to which a product can be added.
/// Disable the Add Product and Cancel buttons.
///
if (-1 == this.orderID)
{
this.btnAddProduct.Enabled = false;
this.btnCancel.Enabled = false;
}
/// Otherwise, there is an order to which the product can be added; enable both the Add Product and
/// Cancel buttons.
///
else
{
this.btnAddProduct.Enabled = true;
this.btnCancel.Enabled = true;
}
/// If there is no inventory, the Product combo box is disabled, and no product image is displayed.
///
if (0 == this.dvInventory.Count)
{
this.productID = -1;
this.cboProduct.Enabled = false;
this.pboProductImage.Image = null;
}
/// Otherwise, enable the product combo box (so users can select products) and update the
/// maximum quantity of the selected product.
///
else
{
this.cboProduct.Enabled = true;
row = (DataRowView)BindingContext[dvInventory].Current;
maxQuantity = Convert.ToInt32(row["Quantity"]);
}
/// Update the max quantity numeric control.
///
UpdateMaxQuantity(maxQuantity);
/// Refresh the user interface.
///
Update();
}
/// Update the subtotal.
///
private void UpdateSubTotal()
{
decimal subTotal = 0;
if (0 != this.lblPriceValue.Text.Length)
{
decimal unitCost = Convert.ToDecimal(this.lblPriceValue.Text);
/// Calculate the subtotal by multiplying the unit cost by the quantity.
///
subTotal = this.numQuantity.Value * unitCost;
}
this.lblSubTotalValue.Text = subTotal.ToString("C");
}
/// Update the max quantity numeric control.
///
private void UpdateMaxQuantity(int maxQuantity)
{
/// If there are one or more products, enable the numeric control, set the minimum value to 1 (the
/// user cannot add less than one product), and set the maximum value to the max quantity.
///
if (0 < maxQuantity)
{
this.numQuantity.Enabled = true;
this.numQuantity.Minimum = 1;
this.numQuantity.Maximum = maxQuantity;
this.numQuantity.Value = 1;
this.numQuantity.Text = "1";
}
/// Otherwise, disable the numeric control and set all properties to 0.
///
else
{
this.numQuantity.Minimum = 0;
this.numQuantity.Maximum = 0;
this.numQuantity.Value = 0;
this.numQuantity.Text = "0";
this.numQuantity.Enabled = false;
}
}
/// Load the product image. The image name is retrieved from the database. The actual image file
/// is on disk on the device.
///
private void LoadProductImage(string productImage)
{
try
{
this.pboProductImage.Image = new Bitmap(this.dataIBuySpy.ProgramPath + @"\" + productImage);
}
catch(Exception e)
{
MessageBox.Show("Load product photo: " + e.Message, "IBuySpy Delivery");
}
}
/// The category combo box allows users to change the category of products that are displayed. Changing the category filters the
/// inventory in the products combo box accordingly.
///
private void cboCategory_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (0 <= this.cboCategory.SelectedIndex &&
this.categoryID != Convert.ToInt32(this.cboCategory.SelectedValue))
{
this.categoryID = Convert.ToInt32(this.cboCategory.SelectedValue);
RefreshInventory();
}
}
/// The product combo box allows the user to select different products to view (and optionally add to the
/// current order). Selecting a specific product updates user
/// interface controls for quantity, subtotal, and product image. The user interface control for price is automatically updated because it is bound
/// to the Inventory data view.
///
private void cboProduct_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (0 <= this.cboProduct.SelectedIndex &&
this.productID != Convert.ToInt32(this.cboProduct.SelectedValue))
{
this.productID = Convert.ToInt32(this.cboProduct.SelectedValue);
/// Set the current binding position.
///
BindingContext[dvInventory].Position = this.cboProduct.SelectedIndex;
/// Get the current row.
///
DataRowView row = (DataRowView)BindingContext[dvInventory].Current;
UpdateMaxQuantity(Convert.ToInt32(row["Quantity"]));
UpdateSubTotal();
LoadProductImage(row["ProductImage"].ToString());
}
}
/// If the user changes the value of the quantity numeric control using the control arrows,
/// update the subtotal accordingly.
///
private void numQuantity_ValueChanged(object sender, System.EventArgs e)
{
UpdateSubTotal();
}
/// If the user changes the value of the quantity numeric control by typing in a new number,
/// update the subtotal accordingly.
///
private void numQuantity_TextChanged(object sender, System.EventArgs e)
{
int quantity;
try
{
quantity = Convert.ToInt32(this.numQuantity.Text);
}
catch
{
quantity = 0;
}
if (this.numQuantity.Minimum > quantity ||
this.numQuantity.Maximum < quantity)
{
MessageBox.Show(String.Format(@"The number you have entered is invalid. Please enter a valid number ({0} - {1}).", this.numQuantity.Minimum, this.numQuantity.Maximum),
"IBuySpy Delivery");
this.numQuantity.Text = this.numQuantity.Minimum.ToString();
this.numQuantity.Value = this.numQuantity.Minimum;
}
UpdateSubTotal();
}
/// Add the selected product to the current order.
///
private void btnAddProduct_Click(object sender, System.EventArgs e)
{
int remainingQuantity = 0;
int productID;
int quantity;
decimal unitCost;
DataRowView row = null;
/// If there is no inventory, return.
///
if (0 == this.dvInventory.Count)
{
return;
}
try
{
quantity = Convert.ToInt32(this.numQuantity.Text);
}
catch
{
quantity = 0;
}
/// Validate the quantity number.
///
if (this.numQuantity.Minimum > quantity ||
this.numQuantity.Maximum < quantity)
{
MessageBox.Show(String.Format(@"The number you have entered is invalid. Please enter a valid number ({0} - {1}).", this.numQuantity.Minimum, this.numQuantity.Maximum),
"IBuySpy Delivery");
this.numQuantity.Text = this.numQuantity.Minimum.ToString();
this.numQuantity.Value = this.numQuantity.Minimum;
UpdateSubTotal();
return;
}
/// Get the current row.
///
row = (DataRowView)BindingContext[this.dvInventory].Current;
productID = Convert.ToInt32(row["ProductID"]);
quantity = Convert.ToInt32(this.numQuantity.Value);
unitCost = Convert.ToDecimal(row["UnitCost"]);
try
{
/// Add the selected product to the dataset.
///
this.dataIBuySpy.AddProduct(this.orderID, productID, row["ModelName"].ToString(), quantity, unitCost);
/// Calculate the remaining quantity after the product has been added.
///
remainingQuantity = Convert.ToInt32(row["Quantity"]) - quantity;
/// If there are one or more of the selected products remaining, update the quantity of the selected
/// product in the dataset.
///
if (0 < remainingQuantity)
{
row["Quantity"] = remainingQuantity;
}
/// Otherwise, delete the selected product from the dataset.
///
else
{
row.Delete();
}
}
catch(SqlCeException err)
{
IBuySpyData.ShowErrors(err);
}
catch(Exception err)
{
MessageBox.Show("Add product: " + err.Message, "IBuySpy Delivery");
}
UpdateMaxQuantity(remainingQuantity);
/// Go back to the Orders control.
///
OnViewOrders(this, new OrderEventArgs());
}
/// Go back to the Orders control.
///
private void btnCancel_Click(object sender, System.EventArgs e)
{
OnViewOrders(this, new OrderEventArgs());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -