public void DisplayBadIngredients(string selectedFood) { allingredientsList = new Food_Repository().GetAll_Ingredients(selectedFood); harmfulFoods(); bad_ingredients_Pnl.Visible = true; bad_ingredients_Pnl.BringToFront(); }
private void harmfulFoods() { badingredientsLb.Items.Clear(); try { if (selectedFood != null) { var result = new Food_Repository().GetBad_Ingredients(allingredientsList); lines = SplitByLength(selectedFood, 50).ToArray(); badingredientsLb.Items.AddRange(lines); badingredientsLb.Items.Add(""); if (result.Count > 0) { badingredientsLb.Items.Add("Harmful Ingredients Detected:"); int i = 1; foreach (var element in result) { if (i - 1 < result.Count - 1) { badingredientsLb.Items.Add($"{i}. {element}"); i++; } else { badingredientsLb.Items.Add($"{i}. {element}"); } } } else { badingredientsLb.Items.Add("No harmful ingredients detected."); } IEnumerable <string> SplitByLength(string selectedFood, int maxLen) { return(Regex.Split(selectedFood, @"(.{1," + maxLen + @"})(?:\s|$)") .Where(x => x.Length > 0) .Select(x => x.Trim())); } } } catch (Exception) { MessageBox.Show("An error has occurred. Please try again."); } }
private void searchBtn_Click(object sender, EventArgs e) { try { if (bad_ingredients_Pnl.Visible == true || all_ingredients_Pnl.Visible == true) { MiddleSearch(); } foodListBox.Items.Clear(); foodListBox.MeasureItem += foodListBox_MeasureItem; foodListBox.DrawItem += foodListBox_DrawItem; string input = inputTxtBox.Text.Trim(); foodItems = new Food_Repository().GetFoods(input); foodItems.OrderBy(item => item); foreach (var element in foodItems) { foodListBox.Items.Add(element + "\n\t"); } if (foodListBox.Items.Count == 0) { foodListBox.Items.Clear(); foodListBox.Items.Add("Sorry. Unknown food item. Please try again."); } } catch (Exception) { if (inputTxtBox.Text == string.Empty) { MessageBox.Show("Input cannot be empty. Please try again."); } else { MessageBox.Show("An error has occurred. Please try again."); } } }