public void ShowInputDialog(string category, string caption, int getId) { Form prompt = new Form() { Width = 300, Height = 160, FormBorderStyle = FormBorderStyle.FixedDialog, Text = caption, StartPosition = FormStartPosition.CenterScreen }; Label categoryLabel = new Label() { Left = 20, Top = 10, Text = category, Width = 200 }; TextBox categoryTextBox = new TextBox() { Left = 20, Top = 35, Width = 200 }; Button confirmation = new Button() { Text = "Ok", Left = 20, Width = 70, Top = 65 }; Button cancel = new Button() { Text = "Cancel", Left = 120, Width = 70, Top = 65 }; //if getId is not equal negative 1 that means its a existing object if (getId != -1) { FoodCategory CategoryId = FoodCategory.FindById(getId); categoryTextBox.Text = CategoryId.Category; } //button click event handler confirmation.Click += (sender, e) => { try { string inputCategoryName = categoryTextBox.Text; string errorMessages = ""; foreach (FoodCategory food in FoodCategory.GetFoodCategory()) { if (categoryTextBox.Text.ToLower() == food.Category.ToLower()) { errorMessages += "Category name already exist please enter another category name\n"; } } if (inputCategoryName == "") { errorMessages += "Category cannot be empty\n"; } if (errorMessages != "") { MessageBox.Show(errorMessages); } else { prompt.DialogResult = DialogResult.OK; if (getId == -1) { FoodCategory newCategory = new FoodCategory(inputCategoryName); newCategory.Save(); GetAllRecord(); } else { FoodCategory CategoryId = FoodCategory.FindById(getId); CategoryId.Category = inputCategoryName; CategoryId.Save(); GetAllRecord(); } } } catch (Exception exp) { MessageBox.Show(exp.Message); } }; cancel.Click += (sender, e) => { prompt.Close(); }; prompt.Controls.Add(categoryLabel); prompt.Controls.Add(categoryTextBox); prompt.Controls.Add(confirmation); prompt.Controls.Add(cancel); prompt.AcceptButton = confirmation; prompt.ShowDialog(); }
public void ShowInputDialog(string itemname, string quantity, string price, string category, string caption, int getId) { Form prompt = new Form() { Width = 400, Height = 650, FormBorderStyle = FormBorderStyle.FixedDialog, Text = caption, StartPosition = FormStartPosition.CenterScreen }; Label itemNameLabel = new Label() { Left = 20, Top = 10, Text = itemname, Width = 300 }; TextBox itemNameTextBox = new TextBox() { Left = 20, Top = 30, Width = 200 }; Label quantityLabel = new Label() { Left = 20, Top = 80, Text = quantity, Width = 300 }; TextBox quantityTextBox = new TextBox() { Left = 20, Top = 100, Width = 200 }; Label priceLabel = new Label() { Left = 20, Top = 150, Text = price }; TextBox priceTextBox = new TextBox() { Left = 20, Top = 170, Width = 200 }; Label categoryLabel = new Label() { Left = 20, Top = 220, Text = category, Width = 300 }; ListBox categoryListBox = new ListBox() { Left = 20, Top = 245, Width = 200 }; PictureBox imagePictureBox = new PictureBox() { Left = 20, Top = 420, Width = 150, Height = 100 }; Label imageLabel = new Label() { Left = 20, Top = 360, Width = 400, Text = "No file is selected" }; Button imageButton = new Button() { Text = "Browse image", Left = 20, Width = 70, Top = 390 }; Button confirmation = new Button() { Text = "Ok", Left = 20, Width = 70, Top = 550 }; Button cancel = new Button() { Text = "Cancel", Left = 120, Width = 70, Top = 550 }; imageButton.Click += (sender, e) => { OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = "JPG Files(*.jpg)|*.jpg|PNG Files(*.png)|*.png|All Files(*.*)|*.*"; if (dlg.ShowDialog() == DialogResult.OK) { imageLabel.Text = dlg.FileName.ToString(); imagePictureBox.ImageLocation = imageLabel.Text; imagePictureBox.SizeMode = PictureBoxSizeMode.StretchImage; } }; foreach (FoodCategory food in FoodCategory.GetFoodCategory()) { categoryListBox.Sorted = true; categoryListBox.Items.Add(food.Category); } //if getId is not equal negative 1 that means its a existing object if (getId != -1) { FoodStock foodId = FoodStock.FindById(getId); FoodCategory categoryId = FoodCategory.FindById(foodId.CategoryId); categoryListBox.SelectedItem = categoryId.Category; itemNameTextBox.Text = foodId.Name; quantityTextBox.Text = foodId.Quantity.ToString(); priceTextBox.Text = foodId.Price.ToString(); imagePictureBox.Image = ConvertBinaryToImage(foodId.Image); imagePictureBox.SizeMode = PictureBoxSizeMode.StretchImage; } //button click event handler confirmation.Click += (sender, e) => { try { string inputItemName = itemNameTextBox.Text; int inputQuantity = Convert.ToInt32(quantityTextBox.Text); decimal inputPrice = Convert.ToDecimal(priceTextBox.Text); int selectedItem = 0; string errorMessages = ""; bool validSendEmail = false; foreach (FoodStock food in FoodStock.GetFoodStocks()) { if (itemNameTextBox.Text.ToLower() == food.Name.ToLower() && getId != food.Id) { errorMessages += "Food name already exist please enter another food name\n"; } } if (categoryListBox.SelectedItem != null) { string selectedItemName = categoryListBox.Items[categoryListBox.SelectedIndex].ToString(); FoodCategory categoryId = FoodCategory.FindByCategory(selectedItemName); selectedItem = categoryId.Id; } else { errorMessages += "Please select one food category\n"; } if (inputQuantity < 0) { errorMessages += "Please enter positive integer for Quantity\n"; } if (inputPrice < 0) { errorMessages += "Please enter positive integer for Price\n"; } if (errorMessages != "") { MessageBox.Show(errorMessages); } else { prompt.DialogResult = DialogResult.OK; if (getId == -1) { byte[] imageByte = null; if (imageLabel.Text == "No file is selected") { Image noImage = Properties.Resources.noimage; using (var ms = new MemoryStream()) { noImage.Save(ms, noImage.RawFormat); imageByte = ms.ToArray(); } } else { FileStream fstream = new FileStream(imageLabel.Text, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fstream); imageByte = br.ReadBytes((int)fstream.Length); } validSendEmail = true; FoodStock newItem = new FoodStock(selectedItem, inputItemName, inputQuantity, inputPrice, validSendEmail, imageByte); newItem.Save(); GetAllRecord(""); } else { FoodStock foodId = FoodStock.FindById(getId); foodId.CategoryId = selectedItem; foodId.Name = inputItemName; if (inputQuantity > foodId.Quantity) { validSendEmail = true; } foodId.Quantity = inputQuantity; foodId.Price = inputPrice; foodId.AllowSendEmail = validSendEmail; byte[] imageByte = null; if (imageLabel.Text != "No file is selected") { FileStream fstream = new FileStream(imageLabel.Text, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fstream); imageByte = br.ReadBytes((int)fstream.Length); foodId.Image = imageByte; } foodId.Save(); GetAllRecord(""); } } } catch { MessageBox.Show("Please fill in all fields with appropriate values and select at least one food category."); } }; cancel.Click += (sender, e) => { prompt.Close(); }; prompt.Controls.Add(itemNameTextBox); prompt.Controls.Add(quantityTextBox); prompt.Controls.Add(priceTextBox); prompt.Controls.Add(itemNameLabel); prompt.Controls.Add(quantityLabel); prompt.Controls.Add(priceLabel); prompt.Controls.Add(categoryLabel); prompt.Controls.Add(categoryListBox); prompt.Controls.Add(imagePictureBox); prompt.Controls.Add(imageLabel); prompt.Controls.Add(imageButton); prompt.Controls.Add(confirmation); prompt.Controls.Add(cancel); prompt.AcceptButton = confirmation; prompt.ShowDialog(); }