Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Search ListBox + Convert ListBox.item to string
#1
I'm working on a project and I have two questions. I searched alot for it but nothing found:

1) how to convert a listbox item to a string (only one item)
2) how to search a listbox and then select the first item that matches the string searching for
... and if it's possible add a complete match option to it

Hope you can help

Greets!

PS: I already know how to search for item <!-- sSmile --><img src="{SMILIES_PATH}/icon_e_smile.gif" alt="Smile" title="Smile" /><!-- sSmile -->
[code2=vbnet]ListBox1.Items.Contains(TextBox1.Text)[/code2]
#2
To answer question #1)
[code2=vbnet]Dim itemString As String = ListBox1.Items.Item(0).ToString[/code2]
Please note that the 0 is an item's index.


To answer question #2)

Something like this would do:
[code2=vbnet]Public Function SearchAndGet(ByVal searchTerm As String) As String
'This method will return the closest result from what we have
For Each item In ListBox1.Items
'Scroll through all items in a listbox
Dim itemText As String = item.ToString 'Get item string
If (itemText.Contains(searchTerm)) Then 'Check to see if item has our search var
Return itemText 'Return the item that contains our search var
End If
Next
Return "" 'If we reach here, there were no matches. Return nothing
End Function[/code2]


If you want to create a program where the user can select "Complete match", you will need to implement all methods into one program. Here is a basic example using a TextBox1, Button1, and ListBox1.
[code2=vbnet]Public Class Form1

Public Function SearchAndGet(ByVal searchTerm As String) As String
'This method will return the closest result from what we have
For Each item In ListBox1.Items
'Scroll through all items in a listbox
Dim itemText As String = item.ToString 'Get item string
If (itemText.Contains(searchTerm)) Then 'Check to see if item has our search var
Return itemText 'Return the item that contains our search var
End If
Next
Return "" 'If we reach here, there were no matches. Return nothing
End Function

Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
Dim listboxMatch As String = SearchAndGet(TextBox1.Text)
If Not (listboxMatch.Equals("")) Then
ListBox1.SelectedItem = listboxMatch
End If
End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If Not (ListBox1.SelectedItem Is Nothing) Then
TextBox1.Text = ListBox1.SelectedItem.ToString
End If
End Sub

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
ListBox1.Items.AddRange({"apples", "oranges", "keyboards", "books", "awesome", "brandon", "bpforums"})
End Sub
End Class[/code2]

The only problem with this code is that it will select words based on letters within the words, not the order that you type them in. So, we can alter this by adjusting our SearchAndGet Method to the following:
[code2=vbnet]Public Function SearchAndGet(ByVal searchTerm As String) As String
Dim possibleMatches As New List(Of String)

'First, let's check the first letters of everything
If (searchTerm.Length <= 0) Then
Return ""
End If

For Each item In ListBox1.Items
Dim firstLetter As String = item.ToString.Substring(0, 1)
If (firstLetter.Equals(searchTerm.Substring(0, 1))) Then
possibleMatches.Add(item.ToString)
End If
Next


If (possibleMatches.Count <= 0) Then
possibleMatches.AddRange(ListBox1.Items)
End If

'This method will return the closest result from what we have
For Each item In possibleMatches
'Scroll through all items in a listbox
Dim itemText As String = item.ToString 'Get item string
If (itemText.Contains(searchTerm)) Then 'Check to see if item has our search var
Return itemText 'Return the item that contains our search var
End If
Next
Return "" 'If we reach here, there were no matches. Return nothing
End Function[/code2]

Please not, all of these methods were drafted with functionality in mind. They are not meant to be fast or optimized.
My Blog | My Setup | My Videos | Have a wonderful day.
#3
haha nice! I already get stuck at listbox.items.contains and you just write the whole code <!-- sConfusedhock: --><img src="{SMILIES_PATH}/icon_eek.gif" alt="Confusedhock:" title="Shocked" /><!-- sConfusedhock: -->
#4
Hahahaha, sorry for overachieving... <!-- sTongue --><img src="{SMILIES_PATH}/icon_razz.gif" alt="Tongue" title="Razz" /><!-- sTongue -->
My Blog | My Setup | My Videos | Have a wonderful day.


Possibly Related Threads…
Thread Author Replies Views Last Post
  NullReferenceException when adding item to generic list brco900033 1 9,395 06-17-2014, 12:16 AM
Last Post: brandonio21
  Problems with dividing a string brco900033 6 21,910 10-26-2013, 07:31 AM
Last Post: brco900033
  Convert 2 lines to one brco900033 2 11,081 02-23-2013, 11:04 AM
Last Post: brco900033
  How to write string query on multiple lines kismetgerald 1 8,463 08-29-2012, 04:01 PM
Last Post: brandonio21
  open text - URL document from listbox and determin which fil Bradley 4 15,771 04-25-2012, 03:07 PM
Last Post: brandonio21
  Outputting a string with quotation marks in it. openeXpressions 15 44,601 12-20-2011, 09:24 PM
Last Post: openeXpressions

Forum Jump:


Users browsing this thread: 1 Guest(s)