简体   繁体   中英

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 ( Below is not the main piece of code for the if statement, the above code is. This was just an experiment on try and extend the code to make it a bit more understandable for myself ):

       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.

What you want to check is whether the item you are currently looking at is a ListBoxItem, and if so, is the contained content a text and is this text equal to your desired text, in order to identify the correct item.

var content = (((x.SelectedItem as ListBoxItem)?.Content as string);
if (content != null && content == "MyDesiredText") {...}

This would be a valid, but not an elegant solution. A better way would be to remember the listbox items while creating them

var desiredListBoxItem = new ListBoxItem(...)
x.AddChild(desiredListBoxItem);

and afterwards, check whether the object references match:

if (x.SelectedItem == desiredListBoxItem) {...}

If you are not updating your item that holds the "Tea + cost" value, you should probably identify it either by string.StartsWith or maybe assigning it with an identifier of your choice. This could be an integer, an enum or another concrete class with predefined instances.

You can do this by using the Tag property for WPF and creating a simple class for Windows Forms ( WPF Tag Property ).

A simple example for Windows Forms would be:

enum FoodType
{
    Tea = 2
}
class FoodItem
{
    public string Text { get; set; }
    public FoodType FoodType { get; set; }

    public override string ToString()
    {
        return Text;
    }
}

When you are adding your items:

listBoxSum.Items.Add(new FoodItem
{
    FoodType = FoodType.Tea,
    Text = "Tea " + teaCost
});

And when you are filtering them:

if (listBoxSum.SelectedItem is FoodItem foodItem && foodItem.FoodType == FoodType.Tea)
{
    // Do work
}

It is even easier for WPF:

enum FoodType
{
    Tea = 1
}

Adding items:

listBoxSum.Items.Add(new ListBoxItem
{
    Content = "Tea " + teaCost,
    Tag = FoodType.Tea
});

Identifying items:

if (listBoxSum.SelectedItem is ListBoxItem foodItem && foodItem.Tag is FoodType foodType && foodType == FoodType.Tea)
{
    MessageBox.Show("hi");
}

So your item in ListBox is called "Tea"?If it is your if statement should look something like this:

if(yourTextBox.Items[yourTextBox.SelectedIndex] == "Tea")
{
    //Your Code
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM