-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
126 lines (115 loc) · 4.31 KB
/
MainWindow.xaml.cs
File metadata and controls
126 lines (115 loc) · 4.31 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
namespace hacksu_grocery_list_lesson
{
// references system.windows.forms
// added namespace system.io
public partial class MainWindow : Window
{
public string groceryselFilePath = Directory.GetCurrentDirectory() + "../../../grocery_selection_list.txt";
public MainWindow()
{
InitializeComponent();
if (File.Exists(groceryselFilePath))
{
string[] existingGrocerySelectionList = File.ReadAllLines(groceryselFilePath);
for (int i = 0; i < existingGrocerySelectionList.Length; i++)
{
grocerySelList.Items.Add(existingGrocerySelectionList[i]);
}
}
}
private void grocerySelNameBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
grocerySelLocationBox.Focus();
}
}
private void grocerySelLocationBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
string grocerySelItem = grocerySelNameBox.Text + " - " + grocerySelLocationBox.Text;
grocerySelList.Items.Add(grocerySelItem);
grocerySelNameBox.Text = "";
grocerySelLocationBox.Text = "";
grocerySelNameBox.Focus();
}
}
private void grocerySelList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (Convert.ToBoolean(addSelBtn.IsChecked))
{
groceryList.Items.Add(grocerySelList.SelectedItem);
}
else if (Convert.ToBoolean(removeSelBtn.IsChecked))
{
grocerySelList.Items.Remove(grocerySelList.SelectedItem);
}
grocerySelList.UnselectAll();
groceryList.UnselectAll();
}
private void groceryList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
groceryList.Items.Remove(groceryList.SelectedItem);
groceryList.UnselectAll();
}
private void clearSelList_Click(object sender, RoutedEventArgs e)
{
grocerySelList.Items.Clear();
}
private void clearList_Click(object sender, RoutedEventArgs e)
{
groceryList.Items.Clear();
}
private void saveList_Click(object sender, RoutedEventArgs e)
{
List<string> grocery_items_list = new List<string>();
foreach (string str in groceryList.Items)
{
grocery_items_list.Add(str);
}
string[] groceryItems = new string[grocery_items_list.Count];
for (int i = 0; i < grocery_items_list.Count; i++)
{
groceryItems[i] = grocery_items_list[i];
}
System.Windows.Forms.SaveFileDialog saveFileDiag = new System.Windows.Forms.SaveFileDialog();
saveFileDiag.Filter = "Text File|*.txt";
saveFileDiag.Title = "Save Grocery List";
saveFileDiag.ShowDialog();
if (saveFileDiag.FileName != "")
{
File.WriteAllLines(saveFileDiag.FileName, groceryItems);
}
}
private void Window_Closed(object sender, EventArgs e)
{
List<string> grocery_items_list = new List<string>();
foreach (string str in grocerySelList.Items)
{
grocery_items_list.Add(str);
}
string[] groceryItems = new string[grocery_items_list.Count];
for (int i = 0; i < grocery_items_list.Count; i++)
{
groceryItems[i] = grocery_items_list[i];
}
File.WriteAllLines(groceryselFilePath, groceryItems);
}
}
}