I am trying to write a program that will tell me how many calories are in a certain number of eggs, cups of flour, cups of sugar, etc. I have a set number of calories for each of the items (i.e. each egg has 72 calories), but I ask the user to input in a textbox how many of each item the recipe calls for. After they input the quantity, I put the item they chose into a listbox followed by a hyphen and the quantity (i.e. eggs (each) - 2). What I need help with is being able to get the computer to determine where the hyphen is in that String and then reading whatever is following the hyphen into a Double value. Is this even possible?

Use the Split method. It takes a string and a delimiter and returns an array of tokes split at the delimiter. For example

Dim test As String = "The-quick-brown-fox"
Dim tokens() As String = test.Split("-")

results in tokens being assigned as follows

tokens(0) = "The"
tokens(1) = "quick"
tokens(2) = "brown"
tokens(3) = "fox"

Ok, so I tried to insert this code and change the variables to match what I have and it didn't work. So here's the part of the code where I need to add this in. Am I just being dense and putting in the wrong thing? Visual Studios says that RcpRqrLstBx.Items() in line 4 is incorrect, but I'm not sure what else to put there??

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim calories As Integer
        
        Dim test As String = RcpRqrLstBx.Items()
        Dim tokens() As String = test.Split("-")
       
        If RcpRqrLstBx.Items.Contains("eggs (each)") Then
            calories = 72 * ?
        End If
        If RcpRqrLstBx.Items.Contains("flour (cups)") Then
            calories = 455 * ?
        End If
        If RcpRqrLstBx.Items.Contains("milk (cups)") Then
            calories = 86 * ?
        End If
        If RcpRqrLstBx.Items.Contains("sugar (cups)") Then
            calories = 774 * ?
        End If
        If RcpRqrLstBx.Items.Contains("butter (tablespoons)") Then
            calories = 102 * ?
        End If
        Label2.Visible = True
        Label2.Text = calories
    End Sub
End Class

RcpRqrLstBx.Items is not a string. It is a collection of ListBox Items. To get at the strings you have to do

for each item as ListBoxItem in ListBox.Items
    dim tokens() as String = item.Text.Split("-")
Next

Each ListBoxItem in ListBox.Items has its own Text property that must be accessed.

Try this

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim calories As Integer = 0

        For i As Integer = 0 To RcpRqrLstBx.Items.Count - 1
            Dim tokens() As String = RcpRqrLstBx.Items.Item(i).Split("-")
            Select Case tokens(0)

                Case "eggs (each)"
                    calories += 72 * Val(tokens(1))
                Case "flour (cups)"
                    calories += 455 * Val(tokens(1))
                    'You Do the rest , I don't LOL

            End Select


        Next

        Label2.Visible = True
        Label2.Text = calories.ToString
    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.