BP Forums
Search ListBox + Convert ListBox.item to string - Printable Version

+- BP Forums (https://bpforums.info)
+-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55)
+--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56)
+---- Forum: VB.NET (Visual Basic 2010/2008) (https://bpforums.info/forumdisplay.php?fid=8)
+----- Forum: Programming Help (https://bpforums.info/forumdisplay.php?fid=9)
+----- Thread: Search ListBox + Convert ListBox.item to string (/showthread.php?tid=643)



Search ListBox + Convert ListBox.item to string - brco900033 - 09-12-2012

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]


Re: Search ListBox + Convert ListBox.item to string - brandonio21 - 09-12-2012

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.


Re: Search ListBox + Convert ListBox.item to string - brco900033 - 09-13-2012

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: -->


Re: Search ListBox + Convert ListBox.item to string - brandonio21 - 09-13-2012

Hahahaha, sorry for overachieving... <!-- sTongue --><img src="{SMILIES_PATH}/icon_razz.gif" alt="Tongue" title="Razz" /><!-- sTongue -->