コード例 #1
0
        private void AddItemButton_Click(object sender, EventArgs e)
        {
            try
            {
                itemPrice = Convert.ToDouble(itemPrice_Str); // try convert to double

                ListViewItem searchItem = null;
                int          index      = 0;
                bool         itemExist  = false;
                do
                {
                    if (index < ItemLV.Items.Count)
                    {
                        searchItem = ItemLV.FindItemWithText(itemName, false, index, false);
                        if (searchItem != null)
                        {
                            itemExist = true;
                            index     = searchItem.Index + 1;
                            break;
                        }
                    }
                    else
                    {
                        searchItem = null;
                    }
                } while (searchItem != null);

                if (itemName != "" && itemName != null && itemPrice >= 0 && itemPrice_Str != null && !itemExist && (itemType != "Food" || itemType != "Beverage"))
                {
                    itemList.Add(new Item()
                    {
                        item_Name = itemName, item_Type = itemType, item_Price = itemPrice
                    });                                                                                              // add to list

                    itemTableAdapter.InsertItem(System.Convert.ToInt32(itemTableAdapter.GetCount()) + 1, itemName, System.Convert.ToDecimal(itemPrice), itemType);

                    itemNameDelete.Items.Add(new { Text = itemName }); // add to delete box

                    ListViewItem listViewItem = new ListViewItem(new string[] { itemName, itemType, itemPrice.ToString("F") });
                    ItemLV.Items.AddRange(new ListViewItem[] { listViewItem }); // add to list view
                }
                else if (itemExist)
                {
                    MessageBox.Show("Item Name already exists", "Warning");
                }
                else
                {
                    MessageBox.Show("A fill area is missing or incorrect", "Warning");
                }
            }
            catch (FormatException) // popup error, havent implement yet
            {
                MessageBox.Show("Item Price is not a decimal", "Warning");
            }
        }
コード例 #2
0
        private void DeleteItemButton_Click(object sender, EventArgs e)
        {
            string itemName = itemNameDelete.Text;

            if (itemName != null) // error proof
            {
                var itemToRemove = itemList.FirstOrDefault(r => r.item_Name == itemName);

                if (itemToRemove != null)
                {
                    itemList.Remove(itemToRemove); // remove from list

                    itemTableAdapter.DeleteItem(System.Convert.ToString(itemName));

                    itemNameDelete.Items.Remove(itemNameDelete.SelectedItem); // remove from delete box

                    // search view list box and delete
                    ListViewItem searchItem = null;
                    int          index      = 0;
                    do
                    {
                        if (index < ItemLV.Items.Count)
                        {
                            searchItem = ItemLV.FindItemWithText(itemName, false, index, false);
                            if (searchItem != null)
                            {
                                ItemLV.Items.Remove(searchItem);
                                index = searchItem.Index + 1;
                            }
                        }
                        else
                        {
                            searchItem = null;
                        }
                    } while (searchItem != null);
                }
                else
                {
                    MessageBox.Show("Item name is not in the list", "Warning");
                }
            }
            else
            {
                MessageBox.Show("A fill is missing or incorrect", "Warning");
            }
            itemNameDelete.Text = "";
        }