简体   繁体   English

C#列表框项目标识

[英]C# listbox item identification

Keep in mind, i am not very experienced with c#. 请记住,我对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). 我正在尝试做一个IF语句,该语句使我可以从列表框中选择一个项目,并使其标识其中的文本(最有可能是字符串)。

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). 因为我对c#不太了解,所以这是我目前对if语句所拥有的(显然第一行是错误的)。

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 ): 我已经尝试制作其他字符串来简化这样的语句( 以下不是if语句的主要代码,上面的代码是。这只是尝试并扩展代码以使其更易于理解的实验我自己 ):

       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. 就我个人而言,我被这个小按钮困住了大约2个小时,弄清楚我将如何使remove按钮与计算一起工作。

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. 您要检查的是您当前正在查看的项目是否是ListBoxItem,如果是,则包含的内容是文本,并且此文本是否等于您想要的文本,以便标识正确的项目。

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. 如果您不更新包含“茶+成本”值的商品,则可能应该通过string.StartsWith进行标识,或者为它分配您所选择的标识符。 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 ). 您可以通过使用WPF的Tag属性并为Windows窗体创建一个简单的类( WPF Tag属性 )来做到这一点。

A simple example for Windows Forms would be: Windows窗体的一个简单示例为:

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: 对于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: 因此,您在ListBox的项目称为“茶”?如果是,则if语句应如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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