-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShoppingListForm.cs
135 lines (125 loc) · 5.62 KB
/
ShoppingListForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ShoppingListApp
{
public partial class ShoppingListForm : Form
{
static public Categories categories;
public ShoppingListForm()
{
InitializeComponent();
// Initialize the app with new items to showcase a demo
// What should happen here is that the app would load
// the data from an XML or JSON file.
Category beverages = new Category(0, "Beverages");
Category fruit = new Category(1, "Fruit");
Category vegetables = new Category(2, "Vegetables");
Category frozen = new Category(3, "Frozen Foods");
categories = new Categories();
beverages.AddItem(0, "Coke");
beverages.AddItem(1, "Diet Coke");
beverages.AddItem(2, "Captain Morgan");
beverages.AddItem(3, "OJ");
beverages.AddItem(4, "Vodka");
fruit.AddItem(0, "Apple");
fruit.AddItem(1, "Grapes");
fruit.AddItem(2, "Orange");
fruit.AddItem(3, "Pomegranate");
fruit.AddItem(4, "Watermelon");
fruit.AddItem(5, "Pear");
fruit.AddItem(6, "Lemon");
fruit.AddItem(7, "Lime");
fruit.AddItem(8, "Strawberry");
vegetables.AddItem(0, "Carrot");
vegetables.AddItem(1, "Celery");
vegetables.AddItem(2, "Lettuce");
frozen.AddItem(0, "Hash Browns");
frozen.AddItem(1, "Hot Pockets");
frozen.AddItem(2, "Frozen Pizza");
categories.AddCategory(beverages);
categories.AddCategory(fruit);
categories.AddCategory(vegetables);
categories.AddCategory(frozen);
// Remove items from the sample design
panelMasterList.Controls.Remove(label2);
panelMasterList.Controls.Remove(label3);
panelMasterList.Controls.Remove(label4);
panelMasterList.Controls.Remove(listBox1);
panelMasterList.Controls.Remove(listBox2);
panelMasterList.Controls.Remove(listBox3);
panelShopTrip.Controls.Remove(label6);
panelShopTrip.Controls.Remove(label7);
panelShopTrip.Controls.Remove(label8);
panelShopTrip.Controls.Remove(listBox4);
panelShopTrip.Controls.Remove(listBox5);
panelShopTrip.Controls.Remove(listBox6);
// Dynamically add the labels and listboxes to the
// Master List tab panel
beverages.AddCatToPanel(panelMasterList);
fruit.AddCatToPanel(panelMasterList);
vegetables.AddCatToPanel(panelMasterList);
frozen.AddCatToPanel(panelMasterList);
// Clear the text in the status label on the Master List tab
labelStatus.Text = "";
}
private void btnAbout_Click(object sender, EventArgs e)
{
// Displays the About box in a modal dialog
AboutBox aboutBox = new AboutBox();
aboutBox.ShowDialog(this);
}
private void btnAddItem_Click(object sender, EventArgs e)
{
// Displays the Add Item dialog
AddItemDialog addItemDialog = new AddItemDialog();
addItemDialog.ShowDialog(this);
// If we got something back, work with it.
if (addItemDialog.CatSelected >= 0)
{
// Add the item name to the appropriate category
categories.CatList[addItemDialog.CatSelected].AddItem(666, addItemDialog.NewItemName);
labelStatus.Text = "Added " + addItemDialog.NewItemName + " to " + addItemDialog.CatName;
}
else if (addItemDialog.CatSelected == -1)
{
// This is a new category! We need to add it.
Category newCat = new Category(categories.CatList.Count, addItemDialog.CatName);
categories.CatList.Add(newCat);
newCat.AddItem(0, addItemDialog.NewItemName);
newCat.AddCatToPanel(panelMasterList);
labelStatus.Text = "Added " + addItemDialog.NewItemName + " to new category " + addItemDialog.CatName;
}
}
private void btnDeleteItems_Click(object sender, EventArgs e)
{
// TODO: NEED TO FIX THIS!!!
// Get confirmation and then delete the select items
var result = MessageBox.Show("Are you sure you want to delete the\nselected items from your Master List?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2);
if (result == DialogResult.Yes)
{
// Time to delete some stuff!
foreach (var category in categories.CatList)
{
if (category.CatListBox.SelectedIndex >= 0)
{
// Something was selected here
for (int i = 0; i < category.CatListBox.SelectedIndices.Count; i++)
{
// Remove the item from the list box
category.CatListBox.Items.RemoveAt(category.CatListBox.SelectedIndices.IndexOf(i));
// Delete the item in the category too
category.RemoveItem(category.CatListBox.SelectedIndices.IndexOf(i));
}
}
}
}
}
}
}