4 Replies - 23610 Views - Last Post: 15 June 2013 - 04:40 AM Rate Topic: -----

#1 nathan192001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-November 07

Type In Textbox To Find Listbox Item

Posted 10 November 2007 - 08:45 PM

Hello there. Basically, I want to be able type some text into a textbox and it will highlight the item in a listbox that matches that text. Strangely, I am only able to search the first item in the listbox, and does not search any other items.

Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
		Dim MatchFound As Boolean = False
		Dim I As Integer = 0
		Do
			' check for a match in this list
			If InStr(lstPlaylist.Items.Item(I), txtSearch.Text, CompareMethod.Text) > 0 Then
				MatchFound = True
				lstPlaylist.SelectedIndex = I
			End If
			I += 1				   ' next item
		Loop Until I > MatchFound
		If Not MatchFound Then
			lstPlaylist.SelectedIndex = -1
		End If
	End Sub


Your help would be greatly appreciated.

nathan192001

Is This A Good Question/Topic? 0
  • +

Replies To: Type In Textbox To Find Listbox Item

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4337
  • View blog
  • Posts: 12,141
  • Joined: 18-April 07

Re: Type In Textbox To Find Listbox Item

Posted 10 November 2007 - 11:05 PM

Hi nathan,

You had a few errors here but nothing major. First change is to just check the item directly using the listbox.items(1).toString call. NO need to go through items to get to item. Secondly, you setup the loop test wrong. You can't compare an integer to a boolean like you were. 1 > true doesn't make sense. Instead put two tests here, one to see if the counter has move through all items and the second to see if an item had been found. Either one terminates the loop.

Check out the code to see what I changed...

Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged

		Dim MatchFound As Boolean = False
		Dim I As Integer = 0

		' Loop until I is greater than or equal to items OR there is a match found
		Do
			' If text is in item, highlight item and mark match as found
			If InStr(lstPlaylist.Items(I).ToString, txtSearch.Text, CompareMethod.Text) > 0 Then
				MatchFound = True
				lstPlaylist.SelectedIndex = I
			End If
			I += 1
		Loop Until (I >= lstPlaylist.Items.Count) Or (MatchFound)

		' If no match was found, deselect
		If Not MatchFound Then
			lstPlaylist.SelectedIndex = -1
		End If
End Sub



Just work your way through the code and you should see where you went wrong. Keep up the good work!

Enjoy!

"At DIC we be item searching code ninjas!" :snap:
Was This Post Helpful? 0
  • +
  • -

#3 nathan192001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-November 07

Re: Type In Textbox To Find Listbox Item

Posted 11 November 2007 - 12:54 AM

Thank you very much for your help, your code worked perfectly.

nathan192001
Was This Post Helpful? 0
  • +
  • -

#4 scotty88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 07-January 09

Re: Type In Textbox To Find Listbox Item

Posted 07 January 2009 - 12:08 PM

What about this(if i get this right, ur searching a listbox for something u wrote in a textbox)

If listbox.Items.Contains(textbox.text) Then
do massive and wonderful things
simple?
Was This Post Helpful? 0
  • +
  • -

#5 lazy.boss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-June 13

Re: Type In Textbox To Find Listbox Item

Posted 15 June 2013 - 04:40 AM

how to use this code ..could u help me ..am just beginer
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1