dimanche 26 août 2018

C# listbox item identification

Keep in mind, i am not very experienced with c#.

I am coding a remove button for a list box and the basic function of removing the selected item works.

listBoxSum.Items.RemoveAt(listBoxSum.SelectedIndex);

I'm trying to make an IF statement which will allow me to select an item from my list-box and have it identify the text inside of it (Most likely a string).

As i don't know too much about c#, this is what i currently have for the if statement (obviously the first line is error).

if (listBoxSum.SelectedItem = "Tea")
        {
            totalCost = totalCost - teaCost;
            txtBox_Amount.Text = totalCost.ToString();
        }

I have tried making other strings to help simplify the statement like:

       string teaSelect = "Tea" + teaCost;
       string selection = (string) listBoxSum.SelectedItem;

       if (selection == teaSelect)
       {
            totalCost = totalCost - teaCost;
            txtBox_Amount.Text = totalCost.ToString();
       }

Please help, i don't know whether i should change how i'm thinking about this or if it an easy fix hiding in plain sight. Personally i have been stumped on this little button for around 2 hours figuring out how i am going to make the remove button work with the calculations.

Aucun commentaire:

Enregistrer un commentaire