/// <summary> /// Add To List function for the Dispense tab. Adds an SKU to either the dispense or delete list, depending on the parameter /// </summary> /// <param name="_isdispensing">True if the user wants to add to the dispense list, /// false if the user wants to add the item to the delete list</param> private void D_AddToList(bool _isdispensing) { DataTable tempTable = new DataTable(); DataTable ddTable; int skuToAdd = 0; TextBox ttb; ListBox tlb; //Set the temporary controls depending on whether we're working with dispense or delete if (_isdispensing) { ttb = tb_D_EnterSKU; tlb = lbox_D_ToDispense; ddTable = dispenseTable; } else { ttb = tb_D_EnterSKUDel; tlb = lbox_D_ToDelete; ddTable = deleteTable; } try { skuToAdd = Convert.ToInt16(ttb.Text); } catch (FormatException) { return; } if (skuToAdd < 0) { return; } //See if the SKU exists in inventory, if not then return without doing anything else try { Mydb.SKUSearch(skuToAdd, tempTable); } catch (InvalidOperationException) { MessageBox.Show("Please open a database before attempting to dispense or delete glasses.", "No Database Open", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return; } if (tempTable.Rows.Count == 0) { MessageBox.Show(String.Format("The specified SKU, {0}, does not exist in inventory.", skuToAdd), "SKU Not Found", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); ttb.Focus(); ttb.SelectAll(); return; } int i = tlb.Items.Count; //input is a valid SKU, find the best place to insert it into the listbox while (i > 0 && (int)(tlb.Items[i - 1]) >= skuToAdd) { i--; } if (i < tlb.Items.Count) { //input already exists, don't add anything else if ((int)(tlb.Items[i]) == skuToAdd) { tlb.SelectedIndex = i; return; } } //Insert item into list in the correct position and include it in the data table ddTable.Merge(tempTable); tlb.Items.Insert(i, skuToAdd); tlb.SelectedIndex = i; ttb.Clear(); ttb.Focus(); }