private void Lbx_ingredient_SelectionChanged(object sender, SelectionChangedEventArgs e)
 {
     if (Lbx_ingredient.SelectedItem != null)
     {
         var ingredient = Lbx_ingredient.SelectedItem.ToString();
         Lbx_ingredient.Visibility  = Visibility.Collapsed;
         Lbx_ingredient.ItemsSource = null;
         Tbx_ingredientName.Text    = ingredient;
         Tbx_quantity.Focus();
         PopulateMeasures(ingredient, "");
     }
 }
 private void ConvertValues(bool fromSelect)
 {
     if (Cbx_measureEdit.SelectedItem != null)
     {
         var measure  = Cbx_measureEdit.SelectedItem.ToString();
         var quantity = Tbx_quantityEdit.Text;
         if (quantity != "")
         {
             Regex regEx = new Regex(@"^\d+\.?\d*$");
             if (!regEx.IsMatch(quantity) || Convert.ToDecimal(quantity) <= 0)
             {
                 MessageBox.Show("Please enter a valid quantity", ":|", MessageBoxButton.OK, MessageBoxImage.Error);
                 Tbx_quantityEdit.Focus();
             }
             if (Cbx_measureEdit.SelectedItem.ToString() == Measurement.gram || Cbx_measureEdit.SelectedItem.ToString() == Measurement.ml)
             {
                 var converted = ConvertToKilos(quantity, measure);
                 quantity = converted[1];
                 //var som = (Convert.ToDecimal(converted[1]));
                 //Decimal.Round(som, 2);
                 measure = converted[0];
             }
             else if (Cbx_measureEdit.SelectedItem.ToString() == Measurement.kg || Cbx_measureEdit.SelectedItem.ToString() == Measurement.liter)
             {
                 if (!fromSelect)
                 {
                     if (Convert.ToInt32(quantity) > 50)
                     {
                         MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show("That is a huge quantity! Are you sure you want to proceed?", "Add ingredient confirmation", System.Windows.MessageBoxButton.YesNo);
                         if (messageBoxResult == MessageBoxResult.No || messageBoxResult == MessageBoxResult.Cancel || messageBoxResult == MessageBoxResult.None)
                         {
                             Tbx_quantity.Focus();
                             Tbx_quantityEdit.Focus();
                             return;
                         }
                     }
                 }
                 var converted = ConvertTomillis(quantity, measure);
                 quantity = converted[1];
                 measure  = converted[0];
             }
             (Lbx_ingredients.SelectedItem as RecipeIngredient).quantity = quantity;
             (Lbx_ingredients.SelectedItem as RecipeIngredient).measure  = measure;
         }
         else
         {
             MessageBox.Show("Please enter a quantity", ":|", MessageBoxButton.OK, MessageBoxImage.Error);
             Tbx_quantity.Focus();
             return;
         }
     }
 }
 private void Tbx_ingredient_KeyDown(object sender, KeyEventArgs e)
 {
     if (e.Key == Key.Enter)
     {
         if (Lbx_ingredient.Items.Count > 0)
         {
             Lbx_ingredient.SelectedIndex = 0;
             Lbx_ingredient.Visibility    = Visibility.Collapsed;
             Tbx_quantity.Focus();
         }
         else if (Lbx_ingredient.ItemsSource == null && Tbx_ingredientName.Text.Length >= 4)
         {
             MessageBox.Show("Please enter a valid ingredient!", ":|", MessageBoxButton.OK, MessageBoxImage.Error);
             Tbx_ingredientName.Text = "";
         }
     }
 }
        private void AddIngredientToList()
        {
            storeData = true;
            var quantity       = Tbx_quantity.Text;
            var ingredientName = Tbx_ingredientName.Text;

            if (quantity != "" || ingredientName != "")
            {
                if (quantity == "")
                {
                    MessageBox.Show("Please enter a quantity", ":|", MessageBoxButton.OK, MessageBoxImage.Error);
                    Tbx_quantity.Focus();
                    return;
                }
                else if (ingredientName == "")
                {
                    MessageBox.Show("Please enter an ingredient!", ":|", MessageBoxButton.OK, MessageBoxImage.Error);
                    Tbx_ingredientName.Focus();
                    return;
                }
                var ing = GetIngDetail(ingredientName, false);
                if (ing != null)
                {
                    if (ing.name == "")
                    {
                        MessageBox.Show("Please enter a valid ingredient!", ":|", MessageBoxButton.OK, MessageBoxImage.Error);
                        Tbx_ingredientName.Text = "";
                    }
                    else
                    {
                        Regex regEx = new Regex(@"^\d+\.?\d*$");
                        if (!regEx.IsMatch(quantity) || Convert.ToDecimal(quantity) <= 0)//newly added Convert.ToInt32(quantity) <= 0
                        {
                            MessageBox.Show("Please enter a valid quantity", ":|", MessageBoxButton.OK, MessageBoxImage.Error);
                            Tbx_quantity.Focus();
                        }
                        else
                        {
                            var measure = Cbx_measure.SelectedValue == null ? "" : Cbx_measure.SelectedValue.ToString();
                            if (measure == Measurement.gram || measure == Measurement.ml)
                            {
                                var converted = ConvertToKilos(quantity, measure);
                                quantity = converted[1];
                                measure  = converted[0];
                            }
                            else if (measure == Measurement.kg || measure == Measurement.liter)
                            {
                                //newly added
                                if (Convert.ToInt32(quantity) > 50)
                                {
                                    MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show("That is a huge quantity! Are you sure you want to proceed?", "Add Ingredient Confirmation", System.Windows.MessageBoxButton.YesNo);
                                    if (messageBoxResult == MessageBoxResult.No || messageBoxResult == MessageBoxResult.Cancel || messageBoxResult == MessageBoxResult.None)
                                    {
                                        Tbx_quantity.Focus();
                                        Tbx_quantityEdit.Focus();
                                        return;
                                    }
                                }

                                var converted = ConvertTomillis(quantity, measure);
                                quantity = converted[1];
                                measure  = converted[0];
                            }
                            var ingredient = new RecipeIngredient
                            {
                                ingredientName = ingredientName,
                                quantity       = quantity,
                                measure        = measure,
                                ingredientType = ing.type
                            };
                            recipeIngredients.Add(ingredient);
                            Sp_Lbx_ingredients.Visibility = Visibility.Visible;
                            Lbx_ingredients.ItemsSource   = recipeIngredients;
                            Lbx_ingredients.SelectedItem  = ingredient;
                            Lbx_ingredients.ScrollIntoView(ingredient);
                            Tbx_quantity.Text = "";
                            Tbx_ingredientName.Focus();
                            Tbx_ingredientName.Text = "";
                            Cbx_measure.ItemsSource = null;
                            Cbx_measure.Visibility  = Visibility.Collapsed;
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Please enter a valid ingredient!", ":|", MessageBoxButton.OK, MessageBoxImage.Error);
                    Tbx_ingredientName.Focus();
                    return;
                }
            }
        }