Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to loop through a listbox items ?

Status
Not open for further replies.

kianang

Technical User
Dec 2, 2002
23
SG
How to I write the coding to loop through a listbox item since I want serach a item from the listbox ?



 
Code:
For each MyItem In MyListbox.Items
   If MyItem.SomeProperty = SomeValue Then
      FoundValue = True;
      Exit For
   End If
Next MyItem
If FoundValue Then
  ' Do something
End If
 
Hi chiph ,sorry that I still new in VB.NET. I dont quite nederstand your coding .
What is Myitem , what is someproperty ? could write the code completely (with declaration)

For example ,I want to search the employee no(textbox) exists in the listbox ? how should i do that ?

Thank you
 
OK, here's a one-liner for you:
Code:
iIndex = MyListBox.FindString("Employee 34")
Here's the online help about this method:

What this method does is locate in your ListBox (the MyListBox object) the first string that starts with the supplied string ("Employee 34" in this example -- you'd use a string variable in your actual code).

If it finds a match, iIndex will contain the index of the item. Otherwise it will contain ListBox.NoMatches (a special value supplied by the ListBox class).

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top