Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
How can I retrieve index range for text searched in a windows listbox in vb.net 2008 ?
I need all or the index range where the entered text matches.
How can it be done ?

Thanx for the reply.

What I want is, if I am searching some text in the listbox, and if it is found, it should give me the indexes or a range of indexes, where it found those ttext?
Posted
Updated 14-Feb-12 6:46am
v2
Comments
Sergey Alexandrovich Kryukov 14-Feb-12 9:43am    
Why do you think there is a range?
--SA

This is the artificial question.

Traditionally, a range is a set if indices Low <= index <= High (range is not a strict term, but usually is in programming in this sense).

The class ListView represents the arbitrary found container of any items. Suppose you have some search criterion which tells you if something is "found" at any of the indices. When you perform the search throughout a whole list, you get some sub-set of indices which can be empty or may or may not form a range. For example, index 0 and 1 match, index to does not match, index 3 matches again. You can consider it as a set of ranges, but not a single range.

After all, this is your search; it looks like you know how to do the search itself, as you did not ask about it. Perform the search and present the result the way you want, depending on the required behavior of your application. By the way, you did not share with us your requirements.

—SA
 
Share this answer
 
created a user control using a textbox and listbox.
You can use this control by binding it to an object or by adding text to it and searching.

Thanx.

Public Class SearchListbox
Dim arr As ArrayList
Dim ds As Object
Dim DispMem As String = ""
Dim ValMem As String = ""

Public WriteOnly Property Datasource() As Object
Set(ByVal value As Object)
lstSearch.DataSource = value
End Set
End Property

Public WriteOnly Property DisplayMember() As String
Set(ByVal value As String)
DispMem = value
lstSearch.DisplayMember = value
End Set
End Property

Public WriteOnly Property ValueMember() As String
Set(ByVal value As String)
ValMem = value
lstSearch.ValueMember = value
End Set
End Property

Private Sub txtSrch_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSrch.TextChanged
Dim i As Integer

If txtSrch.Text.Trim.Length = 0 Then
arr.Clear()
For i = 0 To lstSearch.Items.Count - 1
lstSearch.SetSelected(i, False)
Next i

If lstSearch.Items.Count > 0 Then
lstSearch.SelectedIndex = 0
End If

lblFound.Text = arr.Count & " records..."
Exit Sub
End If

i = lstSearch.FindString(txtSrch.Text, 0)

If i <> -1 Then
arr = New ArrayList

arr.Add(i)
checkChild(i)
End If

For i = 0 To lstSearch.Items.Count - 1
lstSearch.SetSelected(i, False)
Next i

If arr.Count > 0 Then
For i = 0 To arr.Count - 1
lstSearch.SetSelected(arr(i), True)
Next i

lstSearch.TopIndex = arr(0)
End If

lblFound.Text = arr.Count & " records..."
End Sub

Private Sub checkChild(ByVal j As Integer)
Dim k As Integer

For i As Integer = j To lstSearch.Items.Count - 1
k = lstSearch.FindString(txtSrch.Text, i)

If arr.Contains(k) Then
Exit Sub
Else
arr.Add(k)
checkChild(k)
End If
Next
End Sub
 
Share this answer
 
VB
'Textbox1 is a textbox that has been used to allow a random number to be enter in this example
'rather than hardcoding a value
For Each item As Object In ListBox1.Items
    If CType(item, String) = TextBox1.Text Then
        'you can in the parameter of the indexof use either item or
        'ctype(item,string) they both show the same results
        Dim index As Integer = ListBox1.Items.IndexOf(CType(item, String))
        MessageBox.Show(String.Format("Value {0} has been found at index position {1}", TextBox1.Text, index))
    End If
Next


This example shows you how to find a single value and its index within a list box, you could easily modify it to make a collection of the index results.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900