search in listview using textbox

atzdgreat

New member
Joined
Aug 23, 2011
Messages
4
Programming Experience
Beginner
hi! :) anyone have an idea on how to search partial string using textbox from listview in vb.net 2005?

example i have my record in listview:

Apple
Grapes
Mango


now if im going to type string in textbox like "pl" only, then the selected items will be the apple. thanks in advance God bless :)
 
This doesn't really have anything to do with the TextBox. It's just a way to display a String to the user and have them enter a String. A String is a String, regardless of where it comes from.

The ListView does have a method that will search for items and, optionally, subitems whose Text starts with a particular substring, but it will not search within the Text. You simply have to loop through the Items of the ListView and, optionally, the SubItems of each item and test their Text. You can use String.Contains or, if you want case-insensitivity, String.IndexOf or else use the VB Like operator to determine whether a String contains another String.
 
thanks for your reply :) i tried this method .FindItemWithText but nothings happen. can you give me an idea on how to use indexof and looping method or case insensitivity?. :) i just migrate from vb to vb.net. i already did this before in vb6 but wow! it's hard also in vb.net. here's my code

[XCODE] Dim itm As ListViewItem
Dim i As Integer

For i = 0 To lvUser.Items.Count - 1
lvUser.Items.Item(i).BackColor = Color.White
lvUser.Items.Item(i).ForeColor = Color.Black
Next

With lvUser
itm = .FindItemWithText(searchBox.Text, True, 0, True)
'tell me what im going to do in this line for searching that kind of method


If Not itm Is Nothing Then

.Items.Item(itm.Index).EnsureVisible()
.Items.Item(itm.Index).BackColor = Color.CornflowerBlue
.Items.Item(itm.Index).ForeColor = Color.White
.Items.Item(itm.Index).Selected = True


Else
MsgBox("No Record Found!")
For i = 0 To lvUser.Items.Count - 1
lvUser.Items.Item(i).BackColor = Color.White
lvUser.Items.Item(i).ForeColor = Color.Black
Next
.Items(0).EnsureVisible()
.Items.Item(0).BackColor = Color.CornflowerBlue
.Items.Item(0).ForeColor = Color.White
.Items.Item(0).Selected = True

searchBox.SelectionStart = 0
searchBox.SelectionLength = Len(searchBox.Text)
searchBox.Focus()
End If
End With
itm = Nothing

[/XCODE]
 
With regards to formatting your code snippets, you must enter the language option. This leads to the opening tag being
.

As for the question, it doesn't look like you've actually read my post.  I specifically told you that FindItemWithText will only search the beginning, not the middle, of the text.  I also specifically told you that you need to loop through the items and test the Text of each one yourself.

Also, this:[code]lvUser.Items.Item(itm.Index)[/code]is pointless because that simply returns the same object as you started with, i.e. 'itm'.
 
Back
Top