简体   繁体   English

搜索项目具有自定义数据的列表框

[英]Searching a ListBox where Items have customised data

I am loading data into the list box with attached DateTime data. 我正在将数据与附加的DateTime数据一起加载到列表框中。 Now I want to search the listbox for an item that contains tagID (eg: e2003450976543). 现在,我想在列表框中搜索包含tagID的项目(例如:e2003450976543)。 When I search the listbox I cannot find the tagID even though I can see it. 当我搜索列表框时,即使可以看到它也找不到tagID。 I am assuming that the search parameters are not excluding the attached DateTime data. 我假设搜索参数不排除附加的DateTime数据。 Here is my code: 这是我的代码:

class ExpiringItem
{
    private string text;
    public ExpiringItem(string text)
    {
        this.text = text;
        this.Added = DateTime.Now;
    }
    public DateTime Added { get; private set; }
    public override string ToString()
    {
        return text;
    }
}

private void timer1_Tick(object sender, EventArgs e)
{
    for (int i = listBox1.Items.Count -1; i > -1; i--)
    {
        var exp = (ExpiringItem)listBox1.Items[i];
        var timeVisible = DateTime.Now - exp.Added;
        if (timeVisible.TotalSeconds > 30)
            listBox1.Items.RemoveAt(i);

    }
}

To insert I am using: lstTagsHold.Items.Add(new ExpiringItem(txtTagID.Text)); 要插入,我使用的是: lstTagsHold.Items.Add(new ExpiringItem(txtTagID.Text));

Basically all I want to do is check if the tagID exists in the listbox or not... 基本上我要做的就是检查tagID存在于列表框中...

For the test I am using: if (lstHold.Items.Contains(TagID)).. 对于测试,我使用的是: if (lstHold.Items.Contains(TagID))..

I guess the tagID is equal to this.text in your code. 我猜你的代码中的tagID等于this.text

You need to know that ListBox does not contain a list of string, but a list of objects. 您需要知道ListBox不包含字符串列表,而是对象列表。 You can see the tagID because you have override the ToString() method to present the text. 您可以看到tagID,因为您已经重写了ToString()方法来显示文本。

And according to var exp = (ExpiringItem)listBox1.Items[i]; 并根据var exp = (ExpiringItem)listBox1.Items[i]; , the exp.text should be your tagID, but it's private . ,exp.text应该是您的tagID,但它是private Please write a public property for it, and your tagID is there. 请为其编写一个公共属性,并且您的tagID在那里。

The Items.Contains methos test for identity of the param with the object stored in the ListBox.Items. Items.Contains方法对参数与存储在ListBox.Items中的对象的身份进行测试。 But as you don't have the objects but only a string value it can't work, so you need to search through the Items..: 但是由于您没有对象,只有一个字符串值,所以它不起作用,因此您需要搜索 Items ..:

After making text public as JC has noted, you can access it in a function like this: 正如JC所指出的那样,在text public之后,您可以使用以下函数来访问它:

int findFirstID(ListBox lb, string id)
{
    for (int i = 0; i < lb.Items.Count; i++)
    {
        var ei = lb.Items[i] as ExpiringItem;
        // if text is public:
        if (ei.text == id) return i;
        // if it isn't:
        if (ei.ToString()== id) return i;

    }
    return -1;
}

This will return the index of the first item with the ID you search for or -1 if it is not found. 这将返回具有您要搜索的ID的第一项的索引,如果未找到则返回-1。

If all you want is to check if is is in the listBox you can use a little LINQ : 如果您只想检查是否在listBox ,则可以使用LINQ

// if text is public:
if (listBox1.Items.Cast<ExpiringItem>().Where(x => x.text == yourID).Count() > 0) ..

// if it isn't::
if (listBox1.Items.Cast<ExpiringItem>().Where(x => x.ToString()== yourID).Count() > 0) ..

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

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