Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB9.0
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 14-Feb-12 2:25am
Edited 14-Feb-12 7:46am
v2
Comments
SAKryukov at 14-Feb-12 9:43am
   
Why do you think there is a range?
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

    '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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,769
1 Sergey Alexandrovich Kryukov 6,338
2 DamithSL 5,408
3 Manas Bhardwaj 4,816
4 Maciej Los 4,195


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 15 Feb 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100