-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainForm.cs
256 lines (226 loc) · 7.64 KB
/
MainForm.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
* Created by SharpDevelop.
* User: user
* Date: 2011-12-02
* Time: 17:21
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Linq;
using Db4objects.Db4o;
using Db4objects.Db4o.Linq;
namespace Lenders
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
this.refreshItems();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
void refreshItems(){
IEnumerable<Item> items = from Item i in DBConnection.Instance.DB select i;
// listView1.Clear(); Tu byl blad, czyscilo caly komponent
listView1.Items.Clear();
foreach(Item i in items){
ListViewItem lvi = new ListViewItem();
lvi.Text = i.type.Name;
lvi.SubItems.Add(i.Title);
lvi.SubItems.Add(i.BuyDate.ToString());
lvi.SubItems.Add(i.Price.ToString());
lvi.SubItems.Add(i.Place.ToString());
lvi.SubItems.Add(i.IsBorrow.ToString());
if(i.IsBorrow) //condition to show borrowdate
{
lvi.SubItems.Add(i.BorrowDate.ToString());
lvi.SubItems.Add(i.Lender.ToString());
}
listView1.Items.Add(lvi);
}
}
void AddToolStripMenuItemClick(object sender, EventArgs e)
{
AddDialog dlg = new AddDialog();
if(dlg.ShowDialog() == DialogResult.OK)
{
IObjectContainer db = DBConnection.Instance.DB;
IObjectSet result = db.QueryByExample(new ItemType(dlg.TypeBox.Text));
ItemType item = (ItemType)result.Next();
DateTime data = dlg.BuyDatePicker.Value;
db.Store(new Item(item, dlg.TitleBox.Text, data, data, false, (float)dlg.Price.Value,dlg.PlaceBox.Text));
this.refreshItems();
}
}
void SaveToolStripMenuItemClick(object sender, EventArgs e)
{
SaveFileDialog dlg = new SaveFileDialog();
if (dlg.ShowDialog() == DialogResult.OK){
DBConnection.saveDatabase(dlg.FileName);
}
}
void OpenToolStripMenuItemClick(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
DBConnection.setDatabase(dlg.FileName);
this.refreshItems();
}
}
void RecordsToolStripMenuItemClick(object sender, EventArgs e)
{
}
void AddToolStripMenuItem1Click(object sender, EventArgs e)
{
AddPeopleDialog dlg = new AddPeopleDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
String firstname = dlg.firstname.Text;
String lastname = dlg.lastname.Text;
DateTime dateofbirth = dlg.dateTimePicker1.Value;
String city = dlg.city.Text;
String address = dlg.address.Text;
IObjectContainer db = DBConnection.Instance.DB;
db.Store(new Lender(firstname,lastname,dateofbirth,city,address));
}
}
void ManageToolStripMenuItem1Click(object sender, EventArgs e)
{
ManageItemDialog dlg = new ManageItemDialog();
dlg.ShowDialog();
}
void ManageToolStripMenuItemClick(object sender, EventArgs e)
{
PeopleListDialog dlg2 = new PeopleListDialog();
if (dlg2.ShowDialog() == DialogResult.OK)
{
ManagePeopleDialog dlg = new ManagePeopleDialog();
ListView.SelectedListViewItemCollection list = dlg2.listView1.SelectedItems;
ListViewItem item = list[0];
dlg.firstname.Text = item.SubItems[0].Text;
dlg.lastname.Text = item.SubItems[1].Text;
dlg.dateTimePicker1.Value = DateTime.Parse(item.SubItems[2].Text);
dlg.city.Text = item.SubItems[3].Text;
dlg.address.Text = item.SubItems[4].Text;;
IEnumerable<Lender> result = from Lender i in DBConnection.Instance.DB where i.FirstName.Equals(item.SubItems[0].Text) && i.LastName.Equals(item.SubItems[1].Text) select i;
if (dlg.ShowDialog() == DialogResult.OK)
{
foreach(Lender it in result){
it.FirstName = dlg.firstname.Text;
it.LastName = dlg.lastname.Text;
it.BirthDate = dlg.dateTimePicker1.Value;
it.City = dlg.city.Text;
it.Address = dlg.address.Text;
DBConnection.Instance.DB.Store(it);
}
}
}
}
void AddItemTypeToolStripMenuItemClick(object sender, EventArgs e)
{
AddItemTypeDialog dlg = new AddItemTypeDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
String itemtype = dlg.textBox1.Text;
IObjectContainer db = DBConnection.Instance.DB;
db.Store(new ItemType(itemtype));
}
}
void MainFormDoubleClick(object sender, EventArgs e)
{
}
void ListView1ItemActivate(object sender, EventArgs e)
{
ListView.SelectedListViewItemCollection list = this.listView1.SelectedItems;
ManageItemDialog dlg = new ManageItemDialog();
ListViewItem item = list[0];
dlg.TypeBox.Text = item.SubItems[0].Text;
dlg.TitleBox.Text = item.SubItems[1].Text;
dlg.BuyDatePicker.Value = DateTime.Parse(item.SubItems[2].Text);
dlg.Price.Value = decimal.Parse(item.SubItems[3].Text);
dlg.PlaceBox.Text = item.SubItems[4].Text;
IEnumerable<Item> result = from Item i in DBConnection.Instance.DB where i.Title.Equals(item.SubItems[1].Text) && i.type.Name.Equals(item.SubItems[0].Text) select i;
DialogResult res = dlg.ShowDialog();
if ( res== DialogResult.OK)
{
foreach(Item it in result){
it.type = new ItemType(dlg.TypeBox.Text);
it.Title = dlg.TitleBox.Text;
it.BuyDate = dlg.BuyDatePicker.Value;
it.Price = (float)dlg.Price.Value;
it.Place = dlg.PlaceBox.Text;
DBConnection.Instance.DB.Store(it);
}
this.refreshItems();
}
else if( res == DialogResult.No){
foreach(Item it in result){
DBConnection.Instance.DB.Delete(it);
this.refreshItems();
}
}
}
void BorrowUnborrowToolStripMenuItemClick(object sender, EventArgs e)
{
BorrowDialog dlg = new BorrowDialog();
ListView.SelectedListViewItemCollection list = this.listView1.SelectedItems;
if(list.Count == 0){
MessageBox.Show("What you want to borrow? There is nothing selected");
}
else{
ListViewItem item = list[0];
IEnumerable<Item> result = from Item i in DBConnection.Instance.DB where i.Title.Equals(item.SubItems[1].Text) && i.type.Name.Equals(item.SubItems[0].Text) select i;
if (dlg.ShowDialog() == DialogResult.OK)
{
ListView.SelectedListViewItemCollection listp = dlg.listView1.SelectedItems;
if(listp.Count == 0){
MessageBox.Show("Who you want to borrow? There is none selected");
}
else{
ListViewItem itemp = listp[0];
IEnumerable<Lender> resultp = from Lender j in DBConnection.Instance.DB where j.FirstName.Equals(itemp.SubItems[0].Text) && j.LastName.Equals(itemp.SubItems[1].Text) select j;
foreach(Lender ln in resultp) {
foreach(Item it in result){
it.Lender = ln;
it.IsBorrow = true;
it.BorrowDate = dlg.dateTimePicker1.Value;
DBConnection.Instance.DB.Store(it);
}
}
this.refreshItems();
}
}
else if (dlg.DialogResult.Equals(DialogResult.Yes))
{
foreach(Item it in result){
it.IsBorrow = false;
DBConnection.Instance.DB.Store(it);
}
this.refreshItems();
}
}
}
void MainFormLoad(object sender, EventArgs e)
{
}
void NewToolStripMenuItemClick(object sender, EventArgs e)
{
DBConnection.clearDatabase();
this.refreshItems();
}
}
}