Solved

Possible to search through a listbox?

Posted on 2007-11-21
8
174 Views
Last Modified: 2010-04-23
I was wondering, i have a textfield and a listbox.

I want to search this listbox, but not from the first letter. This is becouse i have the Customer ID first, and the user should be able to write in f.eks Jimbo in the textfield, and the listbox automatically jumps down to jimbo EVEN though in the listbox it said "12 Jimbo" where 12 is the customer ID

I have a code for finding the name without the customer ID in front, but it doesnt work when i use the customer ID

Anyone have an idea?
Dim i As Integer = Me.listkunde.FindString(Me.txtkundesok.Text)
 
   
        Me.listkunde.SelectedIndex = i

Open in new window

0
Comment
Question by:leeds2000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 16

Expert Comment

by:gnoon
ID: 20332094
You may need to create you own FindString() function.
Function FindString(ByVal list As ListControl, ByVal searchText As String) As Integer
	Dim i As Integer
	For i=0 To list.Items.Count-1
		If list.Items(i).ToString().IndexOf(searchText) Then Return i
	Next
	Return -1
End Function

Open in new window

0
 
LVL 3

Author Comment

by:leeds2000
ID: 20332101
Ugh, i really stink at functions, how do i now place this inside a button :)
0
 
LVL 16

Expert Comment

by:gnoon
ID: 20332130
Place it in you class, then call it from another function.
Sub button1_Click(sender As Bbject, e As EventArgs)
	listkunde.SelectedIndex = FindString(listkunde, txtkundesok.Text)
End Sub

Open in new window

0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 3

Author Comment

by:leeds2000
ID: 20332153
It only picks the top name, even i write f.eks "Jimbo" in the textfield: and there is a name called jimbo in the list (12 Jimbo Jumbo 98764556) last one is phone number and first number is customer ID

i placed my code in the

    Private Sub txtkundesok_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtkundesok.TextChanged

so it would search when i write, but he only marks the top, and doesnt seem to find anything
0
 
LVL 16

Accepted Solution

by:
gnoon earned 500 total points
ID: 20332460
If the input is 'f.eks Jimbo', the code above will use the all letters to search an item. So, nothing's found.
You need to split the search text and find for each 'f.eks' or 'Jimbo'.
Function FindString(ByVal list As ListControl, ByVal searchText As String) As Integer
	Dim i As Integer
	For i=0 To list.Items.Count-1
		If list.Items(i).ToString().ToLower().IndexOf(searchText.ToLower()) Then Return i
	Next
	Return -1
End Function
 
Private Sub txtkundesok_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtkundesok.TextChanged
	Dim split As String() = txtkundesok.Text.Split(New [Char]() {" "c})
 
	Dim i As Integer = -1
	Dim s As String
	For Each s In split
		If s.Trim() <> "" Then
			i = FindString(listkunde, s)
			If i <> -1 Then Exit For
		End If
	Next s
 
	If i <> -1 Then
		listkunde.SetSelected(i, true)
	Else
		listkunde.SelectedIndex = -1
	End If
End Sub

Open in new window

0
 
LVL 3

Author Comment

by:leeds2000
ID: 20333524
It still just selects the top name when trying to search for "jimbo" which in middle, no search seems to work.

I did manage to get the search to mark the second line though when i wrote the number "1" but thats about it.

I dont understand why the search aint working :/
0
 
LVL 16

Assisted Solution

by:gnoon
gnoon earned 500 total points
ID: 20333687
I'm sorry leeds2000. The FindString should be below code.
    Function FindString(ByVal list As ListBox, ByVal searchText As String) As Integer
        Dim i As Integer
        For i = 0 To list.Items.Count - 1
            If list.Items(i).ToString().ToLower().IndexOf(searchText.ToLower()) <> -1 Then Return i
        Next
        Return -1
    End Function

Open in new window

0
 
LVL 3

Author Comment

by:leeds2000
ID: 20333708
Thanks, worked great :D
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question