If txtQuantity
is empty then you have the Quantity
variable set to 1, else try to parse the value typed and return if it is not a number. Also use all integers for calcs or, if a decimal value is allowed, then use a double also for the TotalCalories
variable.
Still it is not clear what you want to do if the user types a zero in the textbox
Private TotalCalories As Double = 0
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim i As Integer = lstIngredients.SelectedIndex
Dim Quantity As Double
Dim intCount As Integer = 0
If Trim(txtQuantity.Text) = "" Then
Quantity = 1
Else if Not Double.TryParse(txtQuantity.Text, Quantity)
MessageBox.Show("The quantity entered is not numeric. Please add a numeric quantity.")
Return ' or Exit Sub
End If
If intCount < Quantity Then
lstRecipe.Items.Add(Quantity & " " & lstIngredients.Text)
intCount += 1
End If
If lstRecipe.Text = "Eggs(each)" Then
TotalCalories += (Quantity * 72)
ElseIf lstRecipe.Text = "Flour(cups)" Then
TotalCalories += (Quantity * 455)
ElseIf lstRecipe.Text = "Milk(cups)" Then
TotalCalories += (Quantity * 86)
ElseIf lstRecipe.Text = "Sugar(cups)" Then
TotalCalories += (Quantity * 774)
ElseIf lstRecipe.Text = "Butter(tablespoons)" Then
TotalCalories += (Quantity * 102)
End If
End Sub
However I think you need to check against the lstIngredients
instead
If lstIngredients.Text = "Eggs(each)" Then
....