Search a string in record of a ListBox

yulyos

Well-known member
Joined
Jun 4, 2004
Messages
67
Location
Israel
Programming Experience
5-10
Hi,

I am using Visual Basic 2005 Express Edition

I have to find a string in record of a ListBox; the string is in fixed place on the record.

I wrote this code and this code working ok:
VB.NET:
[LEFT][COLOR=blue][FONT=Courier New]For[/FONT][/COLOR][FONT=Courier New] n [COLOR=blue]As[/COLOR] [COLOR=blue]Integer[/COLOR] = 0 [COLOR=blue]To[/COLOR] ListBox1.Items.Count - 1[/FONT]
[FONT=Courier New]   [COLOR=blue]If[/COLOR] Trim(Mid(ListBox1.Items(n), 19, 40)).Trim = TextBox3.Text.Trim [COLOR=blue]Then[/COLOR][/FONT]
[FONT=Courier New]       MessageBox.Show(Trim(Mid(ListBox1.Items(n), 19, 40)))[/FONT]
[FONT=Courier New]   [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR][/FONT]
[COLOR=blue][FONT=Courier New]Next[/FONT][/COLOR][/LEFT]
I feel that I wrote like old style (VB6)

Can somebody improve this code (To VB2005)?
 
personally, i'd have used a datatable for the list items, and filtered them (through the bindingsource) according to what was being searched..
 
You can use the FindString or FindStringExact methods of a listbox or combobox to return the search items index with its list without the need of looping through all of the items in the list. It offers an additional parameter where you can define the start index)

VB.NET:
Dim intListBoxItemIndex As Integer = -1
intListBoxItemIndex = ListBox1.FindString(Textbox1.Text.Trim, 19)

MessageBox.Show(String.Format("Found List Item Index : {0}", intListBoxItemIndex))
 
Back
Top