c# - Searching a ListBox where Items have customised data -


i loading data list box attached datetime data. want search listbox item contains tagid (eg: e2003450976543). when search listbox cannot find tagid though can see it. assuming search parameters not excluding attached datetime data. here 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) {     (int = listbox1.items.count -1; > -1; i--)     {         var exp = (expiringitem)listbox1.items[i];         var timevisible = datetime.now - exp.added;         if (timevisible.totalseconds > 30)             listbox1.items.removeat(i);      } } 

to insert using: lsttagshold.items.add(new expiringitem(txttagid.text));

basically want check if tagid exists in listbox or not...

for test using: if (lsthold.items.contains(tagid))..

the items.contains methos test identity of param object stored in listbox.items. don't have objects string value can't work, need search through items..:

after making text public j.c. has noted, can access in function this:

int findfirstid(listbox lb, string id) {     (int = 0; < lb.items.count; i++)     {         var ei = lb.items[i] expiringitem;         // if text public:         if (ei.text == id) return i;         // if isn't:         if (ei.tostring()== id) return i;      }     return -1; } 

this return index of first item id search or -1 if not found.

if want check if is in listbox can use little linq:

// if text public: if (listbox1.items.cast<expiringitem>().where(x => x.text == yourid).count() > 0) ..  // if isn't:: if (listbox1.items.cast<expiringitem>().where(x => x.tostring()== yourid).count() > 0) .. 

Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -