-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathViewProductsForm.cs
325 lines (245 loc) · 10.6 KB
/
ViewProductsForm.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
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;
using System.IO;
namespace NewPOS
{
public partial class ViewProductsForm : Form
{
public delegate void AddToViewProductsForm();
public event AddToViewProductsForm AddProductsEvent;
public event AddToViewProductsForm AddCategoryEvent;
public event AddToViewProductsForm AddButtonsOnClosingViewProductsFormEvent; // add category buttons to main form when view products form is closed
Database1Entities dbe = new Database1Entities();
summaryreportTableAdapters.tblProductTableAdapter productsstableadapter = new summaryreportTableAdapters.tblProductTableAdapter();
summaryreport.tblProductDataTable productdatatable = new summaryreport.tblProductDataTable();
private DataRow LastDataRow = null;
byte[] dataProduct;
byte[] dataCategory;
public ViewProductsForm()
{
InitializeComponent();
dataGridView1.DataSource = dbe.tblProduct.ToList();
dataGridView1.Columns["categoryId"].Visible = false;
dataGridView1.Columns["tblCategory"].Visible = false;
dataGridView1.Columns["tblTransactionItem"].Visible = false;
dataGridView1.Columns["productId"].Visible = false;
cmbFilterCategory.DataSource = dbe.tblCategory.ToList();
cmbFilterCategory.DisplayMember = "CategoryName";
cmbFilterCategory.ValueMember = "categoryId";
cmbSelectCategoryAddProduct.DataSource = dbe.tblCategory.ToList();
cmbSelectCategoryAddProduct.DisplayMember = "CategoryName";
cmbSelectCategoryAddProduct.ValueMember = "categoryId";
productsstableadapter.Fill(productdatatable);
// resize the column once, but allow the
// users to change it.
this.dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
}
private void btnViewAllProducts_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = dbe.tblProduct.ToList();
}
private void cmbFilterCategory_SelectionChangeCommitted(object sender, EventArgs e)
{
var data = from p in dbe.tblProduct where p.categoryId == (int)cmbFilterCategory.SelectedIndex + 1 select p;
var dataList = data.ToList();
dataGridView1.DataSource = dataList;
}
private void btnAddProduct_Click(object sender, EventArgs e)
{
try
{
tblProduct pro = new tblProduct();
pro.productName = txtProductName.Text;
pro.productPrice = decimal.Parse(txtProductPrice.Text);
pro.categoryId = (int)cmbSelectCategoryAddProduct.SelectedValue;
pro.productImage = dataProduct;
pro.openingstock = Convert.ToInt32(stckamnttxt.Text);
dbe.tblProduct.Add(pro);
dbe.SaveChanges();
MessageBox.Show("Product Saved");
AddProductsEvent += ViewProductsForm_AddProductsEvent;
AddProductsEvent();
}
catch
{
MessageBox.Show("Something went wrong. Please, fill out all of the fields.");
}
}
void ViewProductsForm_AddProductsEvent()
{
dataGridView1.DataSource = dbe.tblProduct.ToList();
}
private void btnUploadProductImage_Click(object sender, EventArgs e)
{
DialogResult dr = openFileDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
dataProduct = new byte[fs.Length];
fs.Read(dataProduct, 0, dataProduct.Length);
fs.Close();
MemoryStream ms = new MemoryStream(dataProduct);
try
{
pbProductImage.Image = Image.FromStream(ms);
}
catch (Exception ioe) { MessageBox.Show("Invalid Image!"); return; }
pbProductImage.SizeMode = PictureBoxSizeMode.StretchImage;
}
}
private void btnAddCategory_Click(object sender, EventArgs e)
{
try
{
if (txtCategoryName.Text == "")
{
MessageBox.Show("Please fill out all the fields");
return;
}
tblCategory cat = new tblCategory();
cat.CategoryName = txtCategoryName.Text;
cat.categoryImage = dataCategory;
dbe.tblCategory.Add(cat);
dbe.SaveChanges();
MessageBox.Show("Category Saved");
AddCategoryEvent += ViewProductsForm_AddCategoryEvent;
AddCategoryEvent();
}
catch { }
}
void ViewProductsForm_AddCategoryEvent()
{
cmbFilterCategory.DataSource = dbe.tblCategory.ToList();
cmbSelectCategoryAddProduct.DataSource = dbe.tblCategory.ToList();
}
private void btnUIploadCategoryImage_Click(object sender, EventArgs e)
{
DialogResult dr = openFileDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
dataCategory = new byte[fs.Length];
fs.Read(dataCategory, 0, dataCategory.Length);
fs.Close();
MemoryStream ms = new MemoryStream(dataCategory);
try
{
pbCategoryImage.Image = Image.FromStream(ms);
}
catch (Exception ioe) { MessageBox.Show("Invalid Image!"); return; }
pbCategoryImage.SizeMode = PictureBoxSizeMode.StretchImage;
}
}
private void ViewProductsForm_FormClosed(object sender, FormClosedEventArgs e)
{
AddButtonsOnClosingViewProductsFormEvent();
this.Size = Screen.PrimaryScreen.WorkingArea.Size;
}
private void gbAddCategory_Enter(object sender, EventArgs e)
{
}
private void ViewProductsForm_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'summaryreport.tblProduct' table. You can move, or remove it, as needed.
this.tblProductTableAdapter.Fill(this.summaryreport.tblProduct);
}
private void regionBindingSource_PositionChanged(object sender, EventArgs e)
{
// if the user moves to a new row, check if the
// last row was changed
BindingSource thisBindingSource =
(BindingSource)sender;
DataRow ThisDataRow =
((DataRowView)thisBindingSource.Current).Row;
if (ThisDataRow == LastDataRow)
{
// we need to avoid to write a datarow to the
// database when it is still processed. Otherwise
// we get a problem with the event handling of
//the DataTable.
throw new ApplicationException("It seems the" +
" PositionChanged event was fired twice for" +
" the same row");
}
UpdateRowToDatabase();
// track the current row for next
// PositionChanged event
LastDataRow = ThisDataRow;
}
private void UpdateRowToDatabase()
{
if (LastDataRow != null)
{
if (LastDataRow.RowState ==
DataRowState.Modified)
{
productsstableadapter.Update(LastDataRow);
}
}
}
private static DialogResult ShowInputDialog(ref string input, string type, string Title)
{
System.Drawing.Size size = new System.Drawing.Size(250, 70);
Form inputBox = new Form();
inputBox.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
inputBox.ClientSize = size;
inputBox.Text = Title;
if (Control.IsKeyLocked(Keys.CapsLock))
{
MessageBox.Show("The Caps Lock key is ON.");
}
System.Windows.Forms.TextBox textBox = new TextBox();
if (type == "password")
{
textBox.PasswordChar = '*';
}
textBox.Size = new System.Drawing.Size(size.Width - 10, 23);
textBox.Location = new System.Drawing.Point(5, 5);
textBox.Text = input;
inputBox.Controls.Add(textBox);
Button okButton = new Button();
okButton.DialogResult = System.Windows.Forms.DialogResult.OK;
okButton.Name = "okButton";
okButton.Size = new System.Drawing.Size(75, 23);
okButton.Text = "&OK";
okButton.Location = new System.Drawing.Point(size.Width - 80 - 80, 39);
inputBox.Controls.Add(okButton);
Button cancelButton = new Button();
cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
cancelButton.Name = "cancelButton";
cancelButton.Size = new System.Drawing.Size(75, 23);
cancelButton.Text = "&Cancel";
cancelButton.Location = new System.Drawing.Point(size.Width - 80, 39);
inputBox.Controls.Add(cancelButton);
inputBox.AcceptButton = okButton;
inputBox.CancelButton = cancelButton;
DialogResult result = inputBox.ShowDialog();
input = textBox.Text;
return result;
}
private void editmode_Click(object sender, EventArgs e)
{
string pass = ".";
ShowInputDialog(ref pass, "password", "Admin Password Input");
if (pass == "terra")
{
MessageBox.Show("Successfully Logged In");
editproducts edit1 = new editproducts();
edit1.Show();
return;
}
}
private void dataGridView1_ColumnDividerDoubleClick(object sender, DataGridViewColumnDividerDoubleClickEventArgs e)
{
AddProductsEvent += ViewProductsForm_AddProductsEvent;
AddProductsEvent();
}
}
}