Connoroo
Connoroo

Reputation: 1

Comparing a Listbox Selected Item

My code:

If Form3.lstOrder.SelectedItem = "Chicken" Then
            total -= ChickenPrice
            Form3.txtTotal.Text = total
End If

The program is supposed to read the selected item in the ListBox and compare to see if it matches 'chicken'; if it is chicken, the price of chicken is then removed from the order total.

However this does not work, can anyone help?

Upvotes: 0

Views: 296

Answers (1)

J. Scott Elblein
J. Scott Elblein

Reputation: 4253

The .SelectedItem property is of type Object, (as opposed to String), and you're trying to compare it directly to a String.

Do this instead:

If Form3.lstOrder.SelectedItem.ToString() = "Chicken" Then
            total -= ChickenPrice
            Form3.txtTotal.Text = total
End If

Here's a reference that may help, as well:

https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.listbox.selecteditem?view=netframework-4.8

Upvotes: 1

Related Questions